Original Post
Hi all. I'm kind of a newbie to multi-threading. I've been reading tons of literature and started experimenting with it. I'm concerned though that the following code may turn into unexpected behaviour (read it to the end, I'm AWARE of the existence of mutexes and critical sections) this is a simplified version of my code ;): Like you can see, the intention is that thread04 would wait for the other threads until MyVar is true. When MyVar = true, all threads can stop. Thread04 has read-only capabilities over MyVar, and thread01-03 will read/write to MyVar. My approach ought to work because eventually MyVar will be true in thread04. But I'm concerned that MyVar remains always false due to cache incoherency: But using mutexes & critical sections should have the same problem, unless the releasing of them invalidates the cache somehow: Should I be concerned by the cache thing? AFAIK, the existing syncronization mechanisms should be used if one thread tried to set MyVar to false, while another one tried to true. Which is not the case. Another use is to avoid reading data that is temporary in an inconsistent state, which isn't also the case. Would this approach work? Or am I doomed? Thanks Cheers Dark Sylinc [Edited by - Matias Goldberg on July 13, 2008 12:35:33 PM]
[source=C]
bool MyVar = false;
void thread01()
{
//Do something...
MyVar = true;
}
void thread02()
{
//Do something...
MyVar = true;
}
void thread03()
{
//Do something...
MyVar = true;
}
void thread04()
{
while( !MyVar );
//Do something else after MyVar==true
}
[source=c]
while( !MyVar ); //Always false, even if it is not
[source=c]
void thread01()
{
Lock();
//Do something...
MyVar = true;
UnLock();
}
void thread02()
{
Lock();
//Do something...
MyVar = true;
UnLock();
}
void thread03()
{
Lock();
//Do something...
MyVar = true;
UnLock();
}
void thread04()
{
Lock();
while( !MyVar );
UnLock();
}