0

I got a couple threads which pass data to each other and process them a little. Once I put synchronization between the last two threads the program started crashing. I don't have much experience with threads so instead of debugging i just commented the whole content of the last thread so it's just running while cycle, the other thread remains the same except for the synchronization part which is just

pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

and which i then also commented which made the app work without crash.

There is absolutely nothing dependent on the mutex or the conditional variable in the rest of the program. It also works if I only comment the pthread_cond_signal(&cond);. Any ideas on what is going on?

Pyjong
  • 3,095
  • 4
  • 32
  • 50

2 Answers2

0

Well, that code doesn't really make any sense. If you didn't change the value of any variables protected by the mutex, what was the sense in signalling the condition variable? Also, you can signal with or without holding the mutex, so the lock/unlock is not needed. (See Condition Variables if you don't understand the basics of what condition variables are for.)

But the only likely ways for it to crash is if cond isn't properly initialized, cond was destroyed, or cond was corrupted by something overwriting its memory.

Community
  • 1
  • 1
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Although the code doesnt do anything, it should not crash just by having it there. I guess i will look for memory corruption then. – Pyjong Oct 27 '11 at 07:14
  • No, it should not crash just by having it there, assuming both the mutex and the condition variable are properly initialized. (A common bug is to have some other chunk of code destroy a synchronization primitive, or object containing one, when it's done with it -- forgetting that other threads may still be using it.) – David Schwartz Oct 27 '11 at 07:17
0

The only way for a crash to ever happen is when you've invoked undefined behavior. This could have happened anywhere in the program prior to the pthread_cond_signal call, or the UB could be in the call itself (by passing the address of an uninitialized condition variable). Without more information, there's no way to know.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711