开发者

Volatile vars and multi-core thread synchronization!

I have several threads executing concurrently and checking a va开发者_如何学JAVAlue of a field in their own object. The field is set by the launch thread like this:

for (i = 0; i < ThreadCount; i++)
{
    ThreadContext[i].MyField = 1;
}

Within each thread then I check the value of this value:

if (MyField == 1)
{
   ...//do something
}

However, I noticed that on a 4 core CPU, some of the (4) running threads need several miliseconds or even longer in order to see the changed MyField. MyField is a single char field. What appears to be happening is that when the memory bus is maxed out by the first thread which detects the change, all other threads may stall almost for the entire duration of the run of the first. (assuming there is enough memory pressure). Only when the first thread eases on memory (and does more with registers), is when other threads also get to see the new value.

I checked the asm and there is no compiler optimization in the way here. Calls go directly to memory. How can this be fixed?

Thanks! Jam


I got feedback from Intel: Yes, that's how it works (no easy fix).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜