d*******n 发帖数: 369 | 1 1.
int i = 0 //i global variable
f1()
{
i++;
}
f2()
{
i = i + 2;
}
f3()
{
create thread(f1);
create thread(f2);
}
Possible values of i?
My answer is that 1, 2, or 3 is all possible |
z****e 发帖数: 2024 | 2 why is 1 possible?
is "i++" an atom operation? |
P********e 发帖数: 2610 | 3 你这条件不全吧
【在 d*******n 的大作中提到】 : 1. : int i = 0 //i global variable : f1() : { : i++; : } : f2() : { : i = i + 2; : }
|
t****t 发帖数: 6806 | 4 c++03 didn't define "atomic" anyway. atomic is defined in c++0x, but
currently it's not implemented (actually partially) in gcc up to 4.5.
if you want atomic, you need to use 3rd party library.
【在 z****e 的大作中提到】 : why is 1 possible? : is "i++" an atom operation?
|
z****e 发帖数: 2024 | 5 恩。
【在 t****t 的大作中提到】 : c++03 didn't define "atomic" anyway. atomic is defined in c++0x, but : currently it's not implemented (actually partially) in gcc up to 4.5. : if you want atomic, you need to use 3rd party library.
|
l******e 发帖数: 12192 | 6 没有看见高喊master,我很震惊
【在 z****e 的大作中提到】 : 恩。
|
z****e 发帖数: 2024 | 7 这点心理素质都没有?震惊帝。
【在 l******e 的大作中提到】 : 没有看见高喊master,我很震惊
|
l******e 发帖数: 12192 | 8 要有个适应过程
【在 z****e 的大作中提到】 : 这点心理素质都没有?震惊帝。
|
z****e 发帖数: 2024 | 9 等我过回头再多喊几声,把你的心理损失弥补回来。
【在 l******e 的大作中提到】 : 要有个适应过程
|
a*****i 发帖数: 268 | |
d*******n 发帖数: 369 | 11 my explanation:
both threads could possibly read i before the "i" is written back by both
threads so they both could get its initial value 0. Then both thread could
write back last so the updated "i" could 1 or 2. Of course, its also
possible that one thread reads after the other writes back then it's 3
【在 d*******n 的大作中提到】 : 1. : int i = 0 //i global variable : f1() : { : i++; : } : f2() : { : i = i + 2; : }
|