boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 关于线程读写全局变量的问题
相关主题
关于多线程编程的一个问题
请问多个线程同时访问同一个内存地址会不会使得程序变慢
重复利用threads的问题
如何GDB调试因pthread_cond_wait()阻塞的线程? (转载)
请教什么时候变量会被load进stack,什么时候进入heap呢?
C++ 全局变量是怎么回事?
问个C语言里面全局变量和本地变量引用问题
老哥使用的一项技术: extern定义全局变量
[bssd]goto的现实根据
python用全局变量能节省程序执行时间吗?
相关话题的讨论汇总
话题: flag话题: volatile话题: atomic话题: thread话题: 线程
进入Programming版参与讨论
1 (共1页)
M**u
发帖数: 10158
1
有一个全局变量flag,初始化为0
然后线程A写这个变量flag=1
另外一个线程B不停地读这个变量
while (flag != 1)
sleep (5); //waiting for flag to set as 1
thread_B_do_real_thing_with_flag_1 ();
没有别的线程操作这个flag,请问这种情况下,需要加锁么?
我感觉pthread_cond_wait做这个更好一点
g**e
发帖数: 6127
2
thread B may never jump out of the loop, even when flag is changed from 0 to
1.
you should use volatile key word for flag.

【在 M**u 的大作中提到】
: 有一个全局变量flag,初始化为0
: 然后线程A写这个变量flag=1
: 另外一个线程B不停地读这个变量
: while (flag != 1)
: sleep (5); //waiting for flag to set as 1
: thread_B_do_real_thing_with_flag_1 ();
: 没有别的线程操作这个flag,请问这种情况下,需要加锁么?
: 我感觉pthread_cond_wait做这个更好一点

M**u
发帖数: 10158
3
在用了volatile,防止compile的优化后,
还需不需要用lock呢?

to

【在 g**e 的大作中提到】
: thread B may never jump out of the loop, even when flag is changed from 0 to
: 1.
: you should use volatile key word for flag.

g**w
发帖数: 969
4
不需要
但是浪费CPU

【在 M**u 的大作中提到】
: 在用了volatile,防止compile的优化后,
: 还需不需要用lock呢?
:
: to

M**u
发帖数: 10158
5
所以其实还是lock要好?谢谢啊

【在 g**w 的大作中提到】
: 不需要
: 但是浪费CPU

g**w
发帖数: 969
6
看thread A的任务是啥,需要多少时间完成
如果B等的时间极短,比如几百,几千个cycle,可以用你的方法,类似spin lock,不停
的poll
如果时间很长,就用kernel mode的event之类,B wait on event, A signal event
when its task complete.
如果介于两者之间,用windows下类似critical section之类的,内部实现就是先spin
一会,如果不能acquire, switch to kernel mode wait.

【在 M**u 的大作中提到】
: 所以其实还是lock要好?谢谢啊
M**u
发帖数: 10158
7
等待的时间比较随机,取决于别的模块是不是正常工作。。。
还是用pthread_cond_wait/pthread_cond_signal了

spin

【在 g**w 的大作中提到】
: 看thread A的任务是啥,需要多少时间完成
: 如果B等的时间极短,比如几百,几千个cycle,可以用你的方法,类似spin lock,不停
: 的poll
: 如果时间很长,就用kernel mode的event之类,B wait on event, A signal event
: when its task complete.
: 如果介于两者之间,用windows下类似critical section之类的,内部实现就是先spin
: 一会,如果不能acquire, switch to kernel mode wait.

t****t
发帖数: 6806
8
use atomic if you can. volatile should work as well, but it is not as good
as atomic. for one thing, atomic R/W includes memory barrier.

【在 M**u 的大作中提到】
: 在用了volatile,防止compile的优化后,
: 还需不需要用lock呢?
:
: to

M**u
发帖数: 10158
9

我觉得这两都是worked,但是我不知道哪一种最好,目前是用atomic了
谢谢了。。。

【在 t****t 的大作中提到】
: use atomic if you can. volatile should work as well, but it is not as good
: as atomic. for one thing, atomic R/W includes memory barrier.

h***i
发帖数: 1970
10
atomic是c++0x吧,你用boost?

【在 M**u 的大作中提到】
: 恩
: 我觉得这两都是worked,但是我不知道哪一种最好,目前是用atomic了
: 谢谢了。。。

相关主题
如何GDB调试因pthread_cond_wait()阻塞的线程? (转载)
请教什么时候变量会被load进stack,什么时候进入heap呢?
C++ 全局变量是怎么回事?
问个C语言里面全局变量和本地变量引用问题
进入Programming版参与讨论
F*******i
发帖数: 190
11
hi thrust,
can you give more insights about the atomic? is it a keyword or something
eles?
thanks!

【在 t****t 的大作中提到】
: use atomic if you can. volatile should work as well, but it is not as good
: as atomic. for one thing, atomic R/W includes memory barrier.

t****t
发帖数: 6806
12
it's part of c++0x. basically it's (volatile when needed) + barrier.

【在 F*******i 的大作中提到】
: hi thrust,
: can you give more insights about the atomic? is it a keyword or something
: eles?
: thanks!

n*********i
发帖数: 567
13
不需要加锁。如果你希望B response快,就像现在这样不停的CHECK。如果间隔时间长
,希望省一些CPU,就加个EVENT之类的。
m*****n
发帖数: 204
14
Volatile is not safe because reads and writes to a volatile variable are not
memory barriers. I remember reading something on William Pugh's web site.
His write up is in the context of java but applies to C as well. Basically,
volatile forces the optimizer to keep the memory access, but it does not
prevent the compiler from moving the read/write around. Therefore, the write
to flag by thread B may be moved forward, to somewhere when the work is not
done yet.

【在 M**u 的大作中提到】
: 恩
: 我觉得这两都是worked,但是我不知道哪一种最好,目前是用atomic了
: 谢谢了。。。

m*****e
发帖数: 4193
15
avoid volatile at all cost.

【在 M**u 的大作中提到】
: 恩
: 我觉得这两都是worked,但是我不知道哪一种最好,目前是用atomic了
: 谢谢了。。。

g**e
发帖数: 6127
16
你说的是c++的? java里的1.5以后没啥不能用的

【在 m*****e 的大作中提到】
: avoid volatile at all cost.
s*******e
发帖数: 432
17
I thought you still need to lock the flag. Thinking about the following
situation: after your thread read flag value into the register and before
setting flag to 1, your program is interrupted. Then thread A is suspended
and registers snapshot is stored. Later the program is resumed and thread B
wake up first. then you get a problem
t****t
发帖数: 6806
18
你没看清他的要求, A是不读flag的, 只写1
B是不写flag的, 只读1
只要顺序不错(即有barrier), 不需要lock

before
B

【在 s*******e 的大作中提到】
: I thought you still need to lock the flag. Thinking about the following
: situation: after your thread read flag value into the register and before
: setting flag to 1, your program is interrupted. Then thread A is suspended
: and registers snapshot is stored. Later the program is resumed and thread B
: wake up first. then you get a problem

1 (共1页)
进入Programming版参与讨论
相关主题
python用全局变量能节省程序执行时间吗?
R似乎根本就没有认真考虑过global variable的改写问题
问个C++编译器如何处理函数内的static 变量
用volatile退出线程对不对?(C++)
Bihai,你就用atmoic完事了
从全局变量到IOC模式
FP over head很高
MPI xl fortran problem on Blue Gene
请问一个多线程与volatile关键字的问题。
请问python能否在一个子程序里边创立独立的命名空间?
相关话题的讨论汇总
话题: flag话题: volatile话题: atomic话题: thread话题: 线程