// ------------------------------- // // -------- 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/25/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. Mutex example used to show how corruption can result if no serialization is done. */ // ----------------------------------------------------------- // #include <iostream.h> #include "vbthread.h" #include "vbmutex.h" // Constants const int LOOPCONSTANT = 100000; const int NUM_THREADS = 10; // Mutex that will be shared by threads within this process vbMutex mutex; // Global variables int i, j, k, l; int uselock = 1; // TRUE if locking shared data class SimpleThread : public vbThread { public: SimpleThread() { } ~SimpleThread() { } private: // Base class interface void *ThreadEntryRoutine(vbThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(vbThread_t *thread) { for(int loop=0; loop < LOOPCONSTANT; ++loop) { if(uselock) { // Lock critical section if uselock is true if(mutex.MutexLock() != 0) { cout << mutex.MutexExceptionMessage() << endl; return ExitThread(thread, 1); } } // Shared data ++i; ++j; ++k; ++l; if(uselock) { if(mutex.MutexUnlock() != 0) { cout << mutex.MutexExceptionMessage() << endl; return ExitThread(thread, 1); } } } return 0; } int main() { SimpleThread t; vbThread_t *thread[NUM_THREADS]; cout << "Creating " << NUM_THREADS << " threads" << endl; for(int loop = 0; loop < NUM_THREADS; ++loop) thread[loop] = t.CreateThread(); cout << "Wait for results" << endl; t.sSleep(10); cout << "Show results" << endl; cout << "Using " << NUM_THREADS << " threads and " << LOOPCONSTANT << " loop constant" << endl; cout << "Values are: (should be " << (NUM_THREADS * LOOPCONSTANT) << ")" << endl; cout << " ==>" << i << ' ' << j << ' ' << k << ' ' << l << endl; cout << "Main thread completed" << endl; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //