我有几个线程同时执行并检查它们自己的对象中的字段值。该字段由启动线程设置,如下所示:
for (i = 0; i < ThreadCount; i++)
{
ThreadContext[i].MyField = 1;
}
在每个线程中,然后我检查该值的值:
if (MyField == 1)
{
...//do something
}
但是,我注意到在 4 核 CPU 上,某些 (4) 正在运行的线程需要几毫秒甚至更长的时间才能排序查看更改后的 MyField。 MyField 是单个字符字段。似乎正在发生的情况是,当检测到更改的第一个线程使内存总线达到最大时,所有其他线程可能几乎在第一个线程运行的整个持续时间内停止。 (假设有足够的内存压力)。只有当第一个线程减少内存占用(并且对寄存器执行更多操作)时,其他线程也才能看到新值。
我检查了asm,这里没有编译器优化的方式。调用直接进入内存。如何解决这个问题?
谢谢!
果酱
I have several threads executing concurrently and checking a value 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
发布评论
评论(1)
我从英特尔得到反馈:是的,这就是它的工作原理(不容易修复)。
I got feedback from Intel: Yes, that's how it works (no easy fix).