// ------------------------------- // // -------- 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. Test program demonstrating how to create thread-specific data. */ // ----------------------------------------------------------- // #include <iostream.h> #include "vbthread.h" // Constants const int NUM_THREADS = 2; // Thread specific data used in this process struct threadSpecific_data_t { threadSpecific_data_t(int i, int j) { threadSpecific1 = i; threadSpecific2 = j; } int threadSpecific1; int threadSpecific2; }; // Thread key vbThreadKey threadSpecificKey; class SimpleThread : public vbThread { public: SimpleThread () { oid = 1; } private: // Base class interface void *ThreadEntryRoutine(vbThread_t *thread); void ThreadExitRoutine(vbThread_t *thread); private: // Functions that use the threadSpecific data void foo(vbThread_t *thread); void bar(vbThread_t *thread); private: vbThreadObjectID oid; }; void *SimpleThread::ThreadEntryRoutine(vbThread_t *thread) // Thread's entry function { thread->SetObjectID(oid++); cout << "Entering thread: " << thread->GetObjectID() << endl; threadSpecific_data_t *gData = \ (threadSpecific_data_t *)thread->GetThreadParm(); if(ThreadSetSpecific(threadSpecificKey, gData) != 0) { cout << "Thread local storage error" << endl; return ExitThread(thread, 1); } foo(thread); return 0; } void SimpleThread::foo(vbThread_t *thread) { threadSpecific_data_t *gData = \ (threadSpecific_data_t *)ThreadGetSpecific(threadSpecificKey); cout << "Thread: " << thread->GetObjectID() << " entering foo(), "; cout << "threadSpecific data = " << gData->threadSpecific1 << ' ' << gData->threadSpecific2 << endl; bar(thread); } void SimpleThread::bar(vbThread_t *thread) { threadSpecific_data_t *gData = \ (threadSpecific_data_t *)ThreadGetSpecific(threadSpecificKey); cout << "Thread: " << thread->GetObjectID() << " entering bar(), "; cout << "threadSpecific data = " << gData->threadSpecific1 << ' ' << gData->threadSpecific2 << endl; } void SimpleThread::ThreadExitRoutine(vbThread_t *thread) { cout << "Thread: " << thread->GetObjectID() << " exiting" << endl; void *parm = thread->GetThreadParm(); // Free the local storage resources delete parm; } int main() { SimpleThread t; vbThread_t *thread[NUM_THREADS]; if(t.ThreadKeyCreate(threadSpecificKey) != 0) { cout << "Thread local storage error!" << endl; cout << "Could not obtain and index." << endl; return 1; } cout << "Creating " << NUM_THREADS << " threads" << endl; int i; for(i = 0; i < NUM_THREADS; i++) { // Create per-thread threadSpecific data and pass it to the thread threadSpecific_data_t *gData = new threadSpecific_data_t(i, ((i+1)*2)); thread[i] = t.CreateThread((void *)gData); t.sSleep(1); // Allow the thread to print their messages in order } cout << "Wait for the threads to complete, and release their resources" << endl; for(i = 0; i < NUM_THREADS; i++) t.JoinThread(thread[i]); // Release the index t.ThreadKeyDelete(threadSpecificKey); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //