// Demonstrate the use of the GThreads library // George F. Riley, Georgia Tech, Spring 2012 // To compile, be sure that gthread.h and gthread.cc are in the same // directory as testGThread.cc, the compile: // g++ -o testGThread testGThread.cc gthread.cc // To run, (for example) // ./testGThread 16 1000 100000 // the above says 16 threads, outerloop 1000, inner loop 100000 // Feel free to experiment with various parameters #include #include "gthread.h" using namespace std; // GThread Mutexes MUST be in global memory gthread_mutex_t coutMutex; void MyThreadThreeArgs(int myId, int count1, int count2) { for (int i = 0; i < count1; ++i) { LockMutex(coutMutex); cout << "Hello from thread " << myId << " count1 " << i << endl; UnlockMutex(coutMutex); for (int j = 0; j < count2; ++j) { } } EndThread(); // Required by the GThreads library } int main(int argc, char** argv) { if (argc < 4) { cout << "Usage: testGthread nThreads count1 count2" << endl; exit(1); } int nThreads = atol(argv[1]); int count1 = atol(argv[2]); int count2 = atol(argv[3]); for (int i = 0; i < nThreads; ++i) { // Start each thread CreateThread(MyThreadThreeArgs, i, count1, count2); } // Now wait for all to complete WaitAllThreads(); cout << "Main exiting" << endl; }