I have worker threads and a master thread, the master thread is linked to the UI and can ask the workers to stop working.
My threads run an "infinite" loop that checks a boolean, which value can be modified by the master to stop all threads.
In my worker threads:
while (not_terminated)
{
/* Process */
}
The master thread :
private void stop_btn_clicked(object sender, RoutedEventArgs e)
{
not_terminated = false;
}
I need my master thread to pause the worker thread, I thought about a read-only mutex that all workers can read but writable only by the master, something like a "shared_mutex" from C++. My current solution is to use Thead.Suspend()
and Thread.Resume()
but they appear to be a very bad practice...
I'm new to C# and I found System.Threading.Mutex but the WaitOne()
function blocks if no signal is sent to the mutex, and in fact I'm looking for the opposite : block if a signal is sent.
How would you do it?
EDIT: I'd like to avoid external libraries