-2

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
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • When/how does `globalstop` become not `false`? Where/how are the mutexes initialized? – Scott Hunter Nov 03 '14 at 20:59
  • The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter – MattiasLarsson Nov 03 '14 at 21:02
  • And the mutex initializations? – Scott Hunter Nov 03 '14 at 21:06
  • mutex initilazion: `pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;` @ScottHunter – MattiasLarsson Nov 04 '14 at 12:46

1 Answers1

0

Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.

You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.

Community
  • 1
  • 1
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter – MattiasLarsson Nov 04 '14 at 18:11
  • I suppose you'd need to look at how they are created; odd that 1 gets in the second *without* getting in the first. – Scott Hunter Nov 04 '14 at 19:01
  • I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter – MattiasLarsson Nov 05 '14 at 09:10
  • Without seeing any of that code, you're on your own. – Scott Hunter Nov 05 '14 at 12:03