由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Help on a multithread question
相关主题
pthread mutex能不能用与thread和process之间looking for c++ programmers
java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?mutex一问
Volatile variables do not provide any atomicity (转载)c++问题
openMP or boost::thread (pthread) for multithreading ?condional variable thread sync 问题 (转载)
Restaurant Reservation System...编程题又一道
mutex和semaphore的差别到底是什么?C++ mutex vs Java synchronized method
谁能推荐一个read-writer lock的C++实现? (转载)Multi-thread可以mutex锁资源,Multi-process怎么锁资源?
请问static variable init的问题?multithread synchronization
相关话题的讨论汇总
话题: gdata话题: main话题: question话题: background
进入Programming版参与讨论
1 (共1页)
C******e
发帖数: 1850
1
I have a mutlithread question need your help. I want to create a thread to
do some task in the background, but the problem is both this background
thread and the main() function will use a common global variable, say gdata.
For example, the background thread changes the value of gdata, and the main
() function uses gdata for some calculation. So how do I make sure the main(
) function uses the up-to-date value of gdata? In other words, how do I
synchronize the value of gdata between the backgrou
r*********r
发帖数: 3195
2
mutex
C******e
发帖数: 1850
3
could you be more specific please? this is something new to me.
m*****e
发帖数: 4193
4

Then you won't get the answer from BBS.
Read some books on multithreading programing first.

【在 C******e 的大作中提到】
: could you be more specific please? this is something new to me.
a****l
发帖数: 8211
5
it seems you only have two threads, one main, one background. Then it's very
simple, no need for "advanced" things like mutex. Just add one global
boolean, when background has a new value and boolean is 0, write the value,
set boolean to 1; the main thread keeps checking boolean, if 1, read value,
finish work, then set boolean to 0.

gdata.
main
main(
)

【在 C******e 的大作中提到】
: I have a mutlithread question need your help. I want to create a thread to
: do some task in the background, but the problem is both this background
: thread and the main() function will use a common global variable, say gdata.
: For example, the background thread changes the value of gdata, and the main
: () function uses gdata for some calculation. So how do I make sure the main(
: ) function uses the up-to-date value of gdata? In other words, how do I
: synchronize the value of gdata between the backgrou

p***o
发帖数: 1252
6
You need to declare the global boolean to be 'volatile' for this to work
with modern compilers on most modern multicore processors. Roughly speaking,
you need to tell the compiler and the processor NOT to reorder the writes
in the background and the read/write in the main thread. Search for the
keyword 'memory barrier', and that's why it's better for him to learn
from some decent books ...

very
,
,

【在 a****l 的大作中提到】
: it seems you only have two threads, one main, one background. Then it's very
: simple, no need for "advanced" things like mutex. Just add one global
: boolean, when background has a new value and boolean is 0, write the value,
: set boolean to 1; the main thread keeps checking boolean, if 1, read value,
: finish work, then set boolean to 0.
:
: gdata.
: main
: main(
: )

1 (共1页)
进入Programming版参与讨论
相关主题
multithread synchronizationRestaurant Reservation System...
有人知道AtomicInteger是如何实现的么mutex和semaphore的差别到底是什么?
Windows XP与Multithreading Programming谁能推荐一个read-writer lock的C++实现? (转载)
Pthread support on Windows XP请问static variable init的问题?
pthread mutex能不能用与thread和process之间looking for c++ programmers
java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?mutex一问
Volatile variables do not provide any atomicity (转载)c++问题
openMP or boost::thread (pthread) for multithreading ?condional variable thread sync 问题 (转载)
相关话题的讨论汇总
话题: gdata话题: main话题: question话题: background