swtbase@gmail.com wrote:
Hi,
My program has two threads; one main thread and other working thread.
My main thread asks the worker thread to terminate if system is
shutting down by making true a boolean value which is a global shared
resource. The main thread only writes to the global value and the
worker thread only reads the same value. In this context, is mutex
use necessary?
I'm going to play the devil's advocate here to argue that for this
SIMPLISTIC need, where one OWNER thread is the writer and the OTHERS
are readers only, then your synchronization needs are very low level,
and you don't need strong synchronization.
For precise synchronization, read Doug's and Ulrich's post.
However, my point is that these are FACTORS that are generally
transparent and applicable across the board, i.e, quantum ideas,
context switching, paging, memory delays, etc.
In fact, I would argue in this SIMPLISTIC CASE where you desire a
thread shutdown, you may not want any precise synchronization delays -
just THROW THE SWITCH and let the system act on it.
Again, note I said ONE OWNER WRITER THREAD and other READER threads.
What you probably more interested in is the final wait for thread
shutdown. Thats generally more important when considering "graceful
shutdowns" for simple cases like this.
Pseudo example:
BOOL PoorManShutdownSignal = FALSE;
void MyThreads()
{
while (!PoorManShutdownSignal) {
.. thread loop ..
}
...