易失性变量和多核线程同步!
我有几个线程同时执行并检查它们自己的对象中的字段值。该字段由启动线程设置,如下所示:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从英特尔得到反馈:是的,这就是它的工作原理(不容易修复)。
I got feedback from Intel: Yes, that's how it works (no easy fix).