void runtest() { const int NUM_THREADS = 5; SimpleThread t; gxThread_t *thread[NUM_THREADS]; cout << "Create " << NUM_THREADS << " threads" << "\n" << flush; int i; for(i = 0; i < NUM_THREADS; i++) { int *tcount = new int; *tcount = i; thread[i] = t.CreateThread((void *)tcount); } // Wait for the all the threads to finish before exiting //cout << "Waiting for all threads before exiting" << "\n" << flush; for(i = 0; i < NUM_THREADS; ++i) t.JoinThread(thread[i]); // Release the memory allocated for each thread for(i = 0; i < NUM_THREADS; ++i) { int *tcount = (int *)thread[i]->GetThreadParm(); delete tcount; tcount = 0; delete thread[i]; thread[i] = 0; } cout << "Final value = " << value << "\n\n" << flush; value = 0; }
// Main thread of execution int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; const int num_threads = 10; gxThread_t *thread[num_threads]; int i; for(i = 0; i < num_threads; i++) { thread[i] = t.CreateThread(); t.sSleep(1); } // Wait for all the threads to finish before exiting for(i = 0; i < num_threads; ++i) { if(t.JoinThread(thread[i]) != 0) cout << "Could not join the thread" << "\n"; if(thread[i]->GetThreadExitCode() != THREAD_EXIT_CODE) cout << "This thread exited with an error" << "\n"; } // Prevent memory leaks for(i = 0; i < num_threads; ++i) delete thread[i]; return 0; // Exit the process }