Reading about cancellation and pthreads, it appears to me that best practice of a thread which loops infinitely is not to cancel the thread but to create a flag that will be checked by the thread.
This situation implies that at some point, a thread might attempt to read the flag while someone is writing to the flag. Is this thread safe? How would I go about making a simultaneous get_flag() and set_flag() safe?