I'm trying to lock two mutexes so that the output of each thread (8 threads total) doesn't mix up.The main part of the code creates these 8 threads and sets the policy to FIFO. It kinda a works but not all the thread outputs. The code underneath is the only function including any mutex of any kind in the whole code.
the code is:
void* print_message_function2( void* x )
{
ostringstream convert;
long int num = (long int) x;
long int counter = 0;
pthread_mutex_lock(&mutex1);
string ThreadId;
convert << num;
ThreadId = convert.str();
cout << "Thread " << ThreadId << " is started";
cout << endl;
pthread_mutex_unlock(&mutex1);
while(globalstop == false)
{
counter++;
}
pthread_mutex_lock(&mutex2);
string LoopCounter;
convert << counter;
LoopCounter = convert.str();
cout << "Thread "<< ThreadId <<" Looped: " << LoopCounter;
cout << endl;
pthread_mutex_unlock(&mutex2);
pthread_exit (NULL);
}
Also a sample output from the bourne shell:
Thread 1 is started
Thread 5 is started
Thread 3 is started
Thread 7 is started
Thread 1 Looped: 1185961319