Redian新闻
>
一道关于SMP and threading 题目
avatar
o*e
2
What's the potential problem in the following code if the code is executed
on a SMP machine.
int counter[2]; /* global variable */
/* thread 0 */
counter[0] = 0;
for (i = 0; i < 500000; i++)
counter[0] += counter[0] * 2;
counter[0] /= 2;
}
/* thread 1 */
counter[1] = 0;
for (i = 0; i < 500000; i++)
counter[1] += counter[1] * 2;
counter[1] /= 2;
}
Threading
avatar
m*l
3
i is not thread safe?
这有意义吗?
counter都是0

【在 o*****e 的大作中提到】
: What's the potential problem in the following code if the code is executed
: on a SMP machine.
: int counter[2]; /* global variable */
: /* thread 0 */
: counter[0] = 0;
: for (i = 0; i < 500000; i++)
: counter[0] += counter[0] * 2;
: counter[0] /= 2;
: }
: /* thread 1 */

avatar
b*c
4
???问什么啊,2个不相干的线程????
avatar
o*e
5
两个线程唯一相关的是
它们share
counter[] array.
原题目如此
初值是0,那永远为0
假设它们都是初值100,那keep increasing ...
也不影响结果。
我猜是memory caching的问题。
不知如何切入。
avatar
b*t
6
google "false sharing"

【在 o*****e 的大作中提到】
: What's the potential problem in the following code if the code is executed
: on a SMP machine.
: int counter[2]; /* global variable */
: /* thread 0 */
: counter[0] = 0;
: for (i = 0; i < 500000; i++)
: counter[0] += counter[0] * 2;
: counter[0] /= 2;
: }
: /* thread 1 */

avatar
b*c
7
zan
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。