#ifdef __sun #include #else #include using namespace std; #endif #include #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // getitimer, setitimer void synchronize(pthread_t tid) { pthread_mutex_lock(&mutex); cout << "In synchronize() for thread " << tid << endl; pthread_mutex_unlock(&mutex); return; } void pthreadMain() { try { while ( 1 ) { pthread_t tid = pthread_self(); //cout << "Thread " << tid << " about to call synchronize()..." << endl; synchronize(tid); //cout << "Thread " << tid << " done..." << endl; } } catch ( ... ) { cout << "--> EXCEPTION ???" << endl; } } int main(int argc, char **argv) { int numThreads; if ( argc == 2 ) { numThreads = atoi(argv[1]); } else { cout << "Usage: server " << '\n'; return(1); } pthread_t *tid = new pthread_t[numThreads]; int idx; for ( idx = 1; idx < numThreads; idx++ ) { if ( pthread_create(tid+idx, NULL,(void*(*)(void*))pthreadMain, NULL) != 0 ) cout << "--> FAILED TO CREATE THREAD!!!" << endl; } for ( idx = 1; idx < numThreads; idx++ ) pthread_join(tid[idx], NULL); cout << "--> EXECUTION COMPLETED" << endl; return(0); }