// 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 }
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; }
int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; char *message1 = "Test message 1"; char *message2 = "Test message 2"; t.message = message1; gxThread_t *thread = t.CreateThread(); t.sSleep(1); // Wait for the first thread to exit delete thread; // Prevent memory leak t.message = message2; thread = t.CreateThread(); t.sSleep(1); // Wait for the second thread to exit delete thread; cout << "\n" << flush; cout << "Testing all the reentrant gxThread sleep functions..." << "\n" << flush; cout << "Sleeping for 1 second" << "\n" << flush; t.sSleep(1); cout << "Sleeping for 1000 milliseconds" << "\n" << flush; t.mSleep(1000); cout << "Sleeping for 1000000 microseconds" << "\n" << flush; t.uSleep(1000000); cout << "Sleeping for 100 microseconds" << "\n" << flush; t.uSleep(100); cout << "Sleeping for 1000000000 nanoseconds" << "\n" << flush; t.nSleep(1000000000); cout << "Sleeping for 100 nanoseconds" << "\n" << flush; t.nSleep(100); return 0; // Exit the process }
int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif const unsigned NUM_WORKERS = 10; SimpleThread *t = new SimpleThread; thrPool *pool = new thrPool; unsigned i; for(i = 0; i < NUM_WORKERS; i++) { t->CreateThread(pool, (void *)i); t->sSleep(1); } t->JoinThread(pool); cout << "\n" << flush; cout << "Destroying the thread pool..." << "\n" << flush; t->DestroyThreadPool(pool); cout << "Creating anther pool of " << NUM_WORKERS << " threads" << "\n" << flush; PausePrg(); display_message = 0; // Do not display any console messages pool = t->CreateThreadPool(NUM_WORKERS); t->sSleep(1); cout << "\n" << flush; cout << "Rebuilding the pool" << "\n" << flush; t->RebuildThreadPool(pool); // Wait for the treads to finish before destroying the pool t->JoinThread(pool); cout << "\n" << flush; cout << "Destroying the thread pool..." << "\n" << flush; if(!pool->IsEmpty()) { t->DestroyThreadPool(pool); } else { cout << "The pool is empty" << "\n" << flush; // If DestroyThreadPool() is not called the pool will not be deleted delete pool; } cout << "Exiting..." << "\n" << flush; delete t; return 0; // Exit the process, terminating all threads }
int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; char *message1 = "The quick brown fox jumps over the lazy dog"; // Test the parameter passing to create function gxThread_t *tptr = t.CreateThread((void *)message1); // Wait for the thread to exit t.sSleep(1); delete tptr; // Prevent memory leaks return 0; // Exit the process }