o*****e 发帖数: 99 | 1 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 |
m********l 发帖数: 4394 | 2 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 */
|
b*****c 发帖数: 1103 | |
o*****e 发帖数: 99 | 4 两个线程唯一相关的是
它们share
counter[] array.
原题目如此
初值是0,那永远为0
假设它们都是初值100,那keep increasing ...
也不影响结果。
我猜是memory caching的问题。
不知如何切入。 |
b******t 发帖数: 965 | 5 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 */
|
b*****c 发帖数: 1103 | |