Question

a question about pthread and mutex.

I have architecture producer consumer with a shared queue.

I have two queue operations: push and pop.

for both of these operation I use a mutex (lock - implementation - unlock).

I did not understand a thing...

  • Just use mutexes?

  • Need I to use signal or wait to wakeup of a thread?

  • When a thread finds the mutex locked, this thread will locked (lock mutex is a blocking operation?)

Was it helpful?

Solution

  • Whenever you are sharing a common resource, using mutex is the best way to go. Sometimes semaphores might be required.
  • You do not need to use a signal to wake up a thread, unless you put it to sleep yourself. Usually if a thread hits a mutex that is locked it will wait. The CPU scheduling algorithm will take care of the thread, and you can be sure that it will be woken up once mutex unlocks without any performance issues.
  • The thread will not be locked once it finds a mutex that is blocked. The thread will simply enter waiting queue until cpu scheduling algorithm decides it should be taken out. But it really depends on your definition of what you percieve as locked.

Also please rephrase a question a little bit, it was hard to understand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top