// // Very simple program demonstrating the use of threads. // // Command-line argument is P (number of threads). // // Each thread writes "hello" message to standard output, using // a lock to be sure only one at a time is performing output. // In this version, the lock is no longer a global variable, but // is passed to the threads as part of their argument. We also // make use of C++ facilities to define some things. // #include #include // has exit(), etc. #include // has pthread_ routines #include "threads-lock.h" // has lockObj class #include "threads-threadWrapper.h" // has threadWrapper class // ---- Class for thread --------------------------------------------- class helloThreadObj { public: typedef helloThreadObj * ptr; // member variables are parameters for thread int myID; // unique ID for thread lockObj * outputLock; // lock for stdout helloThreadObj(const int myID, lockObj * const outputLock) { this->myID = myID; this->outputLock = outputLock; } // this member function is the code the thread should execute void run(void) { outputLock->lock(); cout << "Hello from thread " << myID << endl; outputLock->unlock(); } }; // ---- Main program ------------------------------------------------- int main(int argc, char* argv[]) { if (argc < 2) { cerr << "Usage: " << argv[0] << " numThreads\n"; exit(EXIT_FAILURE); } int P = atoi(argv[1]); // Create lock. lockObj outputLock; // Start P new threads, each running wrapper function, with // a helloThreadObj object as parameter. (The wrapper function // just invokes the object's "run()" method.) helloThreadObj::ptr * threadObj = new helloThreadObj::ptr[P]; // parameters for threads pthread_t * threads = new pthread_t[P]; // "handles" for threads for (int i = 0; i < P; ++i) { threadObj[i] = new helloThreadObj(i, &outputLock); if (pthread_create(&threads[i], NULL, threadWrapper::run, (void *) threadObj[i]) != 0) cerr << "Unable to create thread " << i << endl; } // Wait for all threads to complete. for (int i = 0; i < P; ++i) { if (pthread_join(threads[i], NULL) != 0) cerr << "Unable to perform join on thread " << i << endl; } // Free everything allocated with "new" in this function. for (int i = 0; i < P; ++i) delete threadObj[i]; delete[] threadObj; delete[] threads; cout << "That's all, folks!\n"; return EXIT_SUCCESS; }