// ------------------------------- // // -------- 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/10/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 semaphores */ // ----------------------------------------------------------- // #include <iostream.h> #include <stdlib.h> #include "vbthread.h" #include "vbsema.h" // Global semaphores objects vbSemaphore child_counter; vbSemaphore parent_counter; // Class derived from the abstract vbThread base class class MessageClass : public vbThread { public: MessageClass() { } ~MessageClass() { } public: char *message; private: // Base class interface void *ThreadEntryRoutine(vbThread_t *thread); }; void *MessageClass::ThreadEntryRoutine(vbThread_t *thread) { cout << message << endl << flush; // The up operation increments the semaphore parent_counter.SemaphorePost(); child_counter.SemaphorePost(); return 0; } int main() { MessageClass t; char *message1 = "Test message 1"; char *message2 = "Test message 2"; parent_counter.SemaphoreWait(); child_counter--; // Semaphore value now 0 child_counter--; // Semaphore value now -1 // The child_counter now must be incremented 2 times // for a thread blocked on it to be released t.message = message1; t.CreateThread(); // The wait operation blocks the thread if the semaphore // has a value less than or equal to zero. parent_counter.SemaphoreWait(); t.message = message2; t.CreateThread(); // Force the parent thread to block until both children have // printed their message and executed the following semaphore // up function. child_counter.SemaphoreWait(); // Wait for the last thread to print its message before exiting t.sSleep(1); cout << endl; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //