///////////////////////////////////////////////////////////////////// // Simple.cpp - demonstrate threads with no synchronization // // // // Jim Fawcett, CSE691 - Software Modeling & Analysis, Fall 2000 // ///////////////////////////////////////////////////////////////////// #include #include #include #include #include using namespace std; //----< arguments passed to thread function >------------------ struct ThreadArgList { int threadNum; string threadName; }; //----< thread processing function >--------------------------- unsigned int __stdcall threadProc(void *s) { // retrieve threadProc argument type ThreadArgList *pArgs = (ThreadArgList*)s; // display thread number and name int i; for(i=0; i<25; i++) cout << "\n output #" << i << ": " << ((pArgs->threadName).c_str()) << '\n' << ends; return 0; } // //----< entry point for process >------------------------------ void main() { cout << "\n Demonstrate the Operation of Mult-threaded Code " << "\n without using synchronized access to shared resources" << "\n=======================================================\n\n"; // define arguments to _beginthreadex void* Security = NULL; // no security attributes unsigned int StackSize = 0; // stack size picked by OS unsigned int(__stdcall threadProc)(void*); // name of thread function unsigned int InitFlag = 0; // start running unsigned int ThreadID; // receives thread id void *ArgList; // pointer to argument list // define argument values for thread #1 threadProc ThreadArgList arg1; arg1.threadNum = 1; arg1.threadName = "thread #1"; ArgList = (void*)&arg1; // define an array of handles to threads, needed to wait // for their completion const int NumHandles = 2; HANDLE thHand[NumHandles]; // start first thread thHand[0] = (HANDLE)_beginthreadex( Security, StackSize, threadProc, ArgList, InitFlag, &ThreadID ); // // define argument values for thread #2 threadProc ThreadArgList arg2; arg2.threadNum = 2; arg2.threadName = "thread #2"; ArgList = (void*)&arg2; // start second thread thHand[1] = (HANDLE)_beginthreadex( Security, StackSize, threadProc, ArgList, InitFlag, &ThreadID ); // tell process to wait for both threads to complete // before terminating BOOL fwait = TRUE; // wait for all handles DWORD timeOut = INFINITE; // time out in milliseconds WaitForMultipleObjects(NumHandles, thHand, fwait, timeOut); CloseHandle(thHand[0]); // return resources CloseHandle(thHand[1]); // return resources }