You can't unlock a mutex from a thread other than the one that locked
it. You need semaphores instead! Generally speaking:
- Use mutexes to protect access to shared data between multiple threads
(to "lock" data).
- Use semaphores to cause one thread to wait until another thread has
provided a resource. When one thread is READING/CONSUMING data that
another is WRITING/PRODUCING, a semaphore is a good idea. (Caps because
these are the terms by which you'll hear this kind of thing discussed in
I chose this double lock design, because in theory... it will mean I
don't need to create and destroy threads all the time! I can keep on
using the main thread. And this... I think... should be more CPU
efficient, than a lock, correct?
You are right to keep the worker thread around and not create and
destroy threads all the time, assuming you can keep your total thread
count down to something sane (not 10000 threads).