I have a thread that is started and runs continuously upon launch of the program (I'm using pthreads). I need to send commands to the thread from the main thread. I can set a global variable, but that seems risky because what if the secondary thread and main thread try to access it simultaneously? I can wrap the variable in a mutex, but is that necessary? What if I just put in the main thread a button that executes code like this:
// main thread
if(!trigger_variable)
trigger_variable=1;
Then, in the second thread I use this:
// other thread
if(trigger_variable){
do_something();
trigger_variable=0;
}
Would this be robust enough to avoid using a mutex?