[Dev-C++] Volatile in multithreading?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Philip B. <ph...@bl...> - 2009-08-16 23:23:53
|
Hi folks,
I've been using volatile for flag variables to signal that events have occured between threads, always making sure that only one thread writes to it at a time while another thread regularly checks for its value. Like this:
volatile int global_flag=0;
void func_1()
{// Do work.
// Do work.
global_flag=1;
}
void func2() // In another thread.
{
while(global_flag==0)
{
// Do other stuff if needed.
}
// Flag changed, now we can proceed.
}
This has been working fine on my X86 machine running Windows Xp for months without a glitch, but I've recently read up on volatile and found some articles claiming that this is a poor method to accomplish said goal. All I am trying to achieve is a flag variable that is sure not to be read in the middle of a writing operation, but I'm not certain now that I'm going about it the right way. I use MinGw's heaviest optimization setting. Can anyone shed some light on this perhaps? Would I really need to use a synchronization object like a critical section, or is there in fact a simpler method?
Thanks in advance.
Regards
Philip Bennefall |