// ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: testprog.cpp // Compiler Used: MSVC40, egcs-2.90.29, HP CPP 10.24 // Produced By: Doug Gaer // File Creation Date: 03/25/2000 // Date Last Modified: 05/09/2000 // Copyright (c) 2000 Douglas M. Gaer // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* The VBD and VBS C++ classes are copyright (c) 1997 and 2000 by Douglas M. Gaer. All those who put this code or its derivatives in a commercial product MUST mention this copyright in their documentation for users of the products in which this code or its derivative classes are used. Otherwise, you have the freedom to redistribute verbatim copies of this source code, adapt it to your specific needs, or improve the code and release your improvements to the public provided that the modified files carry prominent notices stating that you changed the files and the date of any change. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE, YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. Simple test program demonstrating the basic operation of the vbThread class using mutex and condition variable synchronization primitives. */ // ----------------------------------------------------------- // #include <iostream.h> #include "vbthread.h" #include "vbmutex.h" #include "vbcond.h" // Constants const int max_reps = 10; // Global variables char buffer; int buffer_has_item = 0; int num_reps; // Global synchronization objects vbMutex mutex; vbCondition condition; // Standalone funciton that works with the reader class inline char make_new_item() { return 'A' + num_reps; } inline void consume_item(char &c) { c = 0; } void WriterFunction(); // Class derived from the abstract vbThread base class class ReaderClass : public vbThread { public: ReaderClass() { } ~ReaderClass() { } private: // Base class interface void *ThreadEntryRoutine(vbThread_t *thread); }; // Program's main thread of execution int main() { // Set the rep count to zero num_reps = 0; ReaderClass t; t.CreateThread(); WriterFunction(); // Wait for the thread to consume the last item before exiting t.sSleep(3); return 1; } void *ReaderClass::ThreadEntryRoutine(vbThread_t *thread) { while(num_reps < max_reps) { mutex.MutexLock(); condition.ConditionTimedWait(&mutex, 1); if(buffer_has_item == 1) { cout << "Comsuing item: " << buffer << endl; consume_item(buffer); buffer_has_item = 0; } mutex.MutexUnlock(); } return 0; } void WriterFunction() { while(num_reps < max_reps) { mutex.MutexLock(); condition.ConditionTimedWait(&mutex, 1); if(buffer_has_item == 0) { buffer = make_new_item(); cout << "Making item: " << buffer << endl; buffer_has_item = 1; num_reps++; } mutex.MutexUnlock(); } } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //