由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问一个多线程与volatile关键字的问题。
相关主题
看了这篇文章,脑子有点不够用了关于多线程编程的一个问题
多线程的程序设计有什么好书推荐? (转载)关于线程读写全局变量的问题
关于多线程锁:锁代码还是锁资源?java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
c++下如何实现多线程?c++posix多线程问题请教
有关objec access path的问题c++多线程的工作面试一般会问哪些问题?
问个C++编译器如何处理函数内的static 变量round function in math
C 多线程的一个问题关于Dword 和 word
请教一个c语言实现多线程的问题多线程编程前景如何?
相关话题的讨论汇总
话题: global话题: var话题: volatile话题: mutex话题: c++
进入Programming版参与讨论
1 (共1页)
r****o
发帖数: 1950
1
请问,在multithreading环境下,假如用了mutex来保护某个全局变量,是不是对该全局
变量仍然要加volatile关键字才能保证该变量的值是没有被其他线程改过的?
x****u
发帖数: 44466
2
不需要,两回事。

全局

【在 r****o 的大作中提到】
: 请问,在multithreading环境下,假如用了mutex来保护某个全局变量,是不是对该全局
: 变量仍然要加volatile关键字才能保证该变量的值是没有被其他线程改过的?

X****r
发帖数: 3557
3
一般来说是不需要的。你调用mutex的函数本身就保证了编译器不会假设这个变量未变
化过。

全局

【在 r****o 的大作中提到】
: 请问,在multithreading环境下,假如用了mutex来保护某个全局变量,是不是对该全局
: 变量仍然要加volatile关键字才能保证该变量的值是没有被其他线程改过的?

x****u
发帖数: 44466
4
问题不在这里,这个场合下绝对不需要。
volatile的意思是要让编译器知道此变量可能被外界改变,这是和同步互斥完全不同的
另一个话题。
只要用了mutex保护,这个变量就线程安全了,至于这个变量在内存里面,在寄存器里
面或者被优化掉根本不存在了,都不影响程序的语义。

【在 X****r 的大作中提到】
: 一般来说是不需要的。你调用mutex的函数本身就保证了编译器不会假设这个变量未变
: 化过。
:
: 全局

P********e
发帖数: 2610
5
我觉的你说的不准确
需要不需要取决于这个变量巡行环境在什么系统,比如在embedded system很可能就需
要了
PC下除非还存在register变量。

【在 X****r 的大作中提到】
: 一般来说是不需要的。你调用mutex的函数本身就保证了编译器不会假设这个变量未变
: 化过。
:
: 全局

x****u
发帖数: 44466
6
和系统无关,这题可以算是陷阱了。

【在 P********e 的大作中提到】
: 我觉的你说的不准确
: 需要不需要取决于这个变量巡行环境在什么系统,比如在embedded system很可能就需
: 要了
: PC下除非还存在register变量。

t****t
发帖数: 6806
7
what xentar said is exactly *why* mutex can keep global variable thread-safe
. usually mutex are implemented with opaque functions which compiler has no
knowledge. potentially, compiler thinks mutex function may actually do any c
hange to global functions, and therefore preventing optimizations from happe
ning, such as reordering accessing, caching, etc.
there might exist situations where global variables are accessed without mut
ex protection, such as lock-free concurrent data structures. in th

【在 x****u 的大作中提到】
: 问题不在这里,这个场合下绝对不需要。
: volatile的意思是要让编译器知道此变量可能被外界改变,这是和同步互斥完全不同的
: 另一个话题。
: 只要用了mutex保护,这个变量就线程安全了,至于这个变量在内存里面,在寄存器里
: 面或者被优化掉根本不存在了,都不影响程序的语义。

t****t
发帖数: 6806
8
come on man, you don't even have a clue.

【在 P********e 的大作中提到】
: 我觉的你说的不准确
: 需要不需要取决于这个变量巡行环境在什么系统,比如在embedded system很可能就需
: 要了
: PC下除非还存在register变量。

x****u
发帖数: 44466
9
这个编译器是不知道mutex和普通函数的区别的。。。
我举个例子,如果程序这么写。
lock();
int a = 12345;
unlock();
int b = a + 1;
如果a没有在任何地方被引用,不加volatile的话,那么这个a很可能最后根本就不存在
了,但程序语义没问题。
加了volatile后允许我们使用某些变态的方式使用a,比方说让另一个进程远程搜a的值
,然后修改内存。

safe
no
c
happe
mut
v
synchronizat

【在 t****t 的大作中提到】
: what xentar said is exactly *why* mutex can keep global variable thread-safe
: . usually mutex are implemented with opaque functions which compiler has no
: knowledge. potentially, compiler thinks mutex function may actually do any c
: hange to global functions, and therefore preventing optimizations from happe
: ning, such as reordering accessing, caching, etc.
: there might exist situations where global variables are accessed without mut
: ex protection, such as lock-free concurrent data structures. in th

P********e
发帖数: 2610
10
你才没clue呢
volatile保证不被优化,mutex怎么"preventing optimizations from happening"

safe
no
c
happe
mut
v
synchronizat

【在 t****t 的大作中提到】
: what xentar said is exactly *why* mutex can keep global variable thread-safe
: . usually mutex are implemented with opaque functions which compiler has no
: knowledge. potentially, compiler thinks mutex function may actually do any c
: hange to global functions, and therefore preventing optimizations from happe
: ning, such as reordering accessing, caching, etc.
: there might exist situations where global variables are accessed without mut
: ex protection, such as lock-free concurrent data structures. in th

相关主题
问个C++编译器如何处理函数内的static 变量关于多线程编程的一个问题
C 多线程的一个问题关于线程读写全局变量的问题
请教一个c语言实现多线程的问题java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
进入Programming版参与讨论
t****t
发帖数: 6806
11
yes, compiler has no idea of mutex functions (that's why i said it's opaque)
. but your example is horrible because OP was asking about global objects. g
lobal objects will not be optimized away anyway.
mutex functions has two important features. (a) it must be opaque so that th
e compiler will not do cross-function optimizations. (b) some of them will i
nclude memory access barriers so that CPU will not do out-of-order memory ac
cess across barriers.

【在 x****u 的大作中提到】
: 这个编译器是不知道mutex和普通函数的区别的。。。
: 我举个例子,如果程序这么写。
: lock();
: int a = 12345;
: unlock();
: int b = a + 1;
: 如果a没有在任何地方被引用,不加volatile的话,那么这个a很可能最后根本就不存在
: 了,但程序语义没问题。
: 加了volatile后允许我们使用某些变态的方式使用a,比方说让另一个进程远程搜a的值
: ,然后修改内存。

X****r
发帖数: 3557
12
e.g.
int global_var;
void func() {
int x;
global_var = 1;
pthread_mutex_lock(&mutex);
// At this point, the compiler has no idea whether or not
// pthread_mutex_lock changes global_var, so it has to assume
// it does, thus unable to simply assign 1 to x below.
x = gloabl_var;
pthread_mutex_unlock(&mutex);
}

【在 P********e 的大作中提到】
: 你才没clue呢
: volatile保证不被优化,mutex怎么"preventing optimizations from happening"
:
: safe
: no
: c
: happe
: mut
: v
: synchronizat

x****u
发帖数: 44466
13
你误会了,我这个例子里面a根本不是global的,相反,它是局部变量。
这个mutex是给第二个进程用的,而不是作为线程互斥对象。我举这个例子是说明什么
场合下volatile有意义。第二个程序可以用OS功能suspend第一个进程,然后搜索a的值
,接着修改它。
如果一个变量只对单一程序内有意义,我们不应该阻止它被优化。

opaque)
g
th
i
ac
存在
的值

【在 t****t 的大作中提到】
: yes, compiler has no idea of mutex functions (that's why i said it's opaque)
: . but your example is horrible because OP was asking about global objects. g
: lobal objects will not be optimized away anyway.
: mutex functions has two important features. (a) it must be opaque so that th
: e compiler will not do cross-function optimizations. (b) some of them will i
: nclude memory access barriers so that CPU will not do out-of-order memory ac
: cess across barriers.

x****u
发帖数: 44466
14
你把lock去掉编译器也不敢随便优化。

【在 X****r 的大作中提到】
: e.g.
: int global_var;
: void func() {
: int x;
: global_var = 1;
: pthread_mutex_lock(&mutex);
: // At this point, the compiler has no idea whether or not
: // pthread_mutex_lock changes global_var, so it has to assume
: // it does, thus unable to simply assign 1 to x below.
: x = gloabl_var;

t****t
发帖数: 6806
15
of course it will...

【在 x****u 的大作中提到】
: 你把lock去掉编译器也不敢随便优化。
P********e
发帖数: 2610
16
所以你的意思compiler treat mutex differently
如果把你程序改变成这样:
int x;
gv = 1;
foo();//whatever function
x = gv;
foo();
我还是不觉得mutex能取代volatile

【在 X****r 的大作中提到】
: e.g.
: int global_var;
: void func() {
: int x;
: global_var = 1;
: pthread_mutex_lock(&mutex);
: // At this point, the compiler has no idea whether or not
: // pthread_mutex_lock changes global_var, so it has to assume
: // it does, thus unable to simply assign 1 to x below.
: x = gloabl_var;

P********e
发帖数: 2610
17

opaque)
g
global objects也可以被优化,你说的不对。
th
i
ac

【在 t****t 的大作中提到】
: yes, compiler has no idea of mutex functions (that's why i said it's opaque)
: . but your example is horrible because OP was asking about global objects. g
: lobal objects will not be optimized away anyway.
: mutex functions has two important features. (a) it must be opaque so that th
: e compiler will not do cross-function optimizations. (b) some of them will i
: nclude memory access barriers so that CPU will not do out-of-order memory ac
: cess across barriers.

t****t
发帖数: 6806
18
exactly opposite: compiler just treat mutex as opaque function.
if foo() is opaque, compiler will treat it the same as mutex function. howev
er as i said, mutex function have to include memory barrier which foo() prob
ably don't have.

【在 P********e 的大作中提到】
: 所以你的意思compiler treat mutex differently
: 如果把你程序改变成这样:
: int x;
: gv = 1;
: foo();//whatever function
: x = gv;
: foo();
: 我还是不觉得mutex能取代volatile

X****r
发帖数: 3557
19
int global_var;
void func() {
int x;
gloabl_var = 1;
x = global_var;
}
你说上面的例子里编译器不会把x = global_var优化成x = 1?

【在 x****u 的大作中提到】
: 你把lock去掉编译器也不敢随便优化。
P********e
发帖数: 2610
20
我同意memory barrier,但如果是register variable呢?

howev
prob

【在 t****t 的大作中提到】
: exactly opposite: compiler just treat mutex as opaque function.
: if foo() is opaque, compiler will treat it the same as mutex function. howev
: er as i said, mutex function have to include memory barrier which foo() prob
: ably don't have.

相关主题
c++posix多线程问题请教关于Dword 和 word
c++多线程的工作面试一般会问哪些问题?多线程编程前景如何?
round function in math多线程 编程,process 和 thread 的一些问题。
进入Programming版参与讨论
x****u
发帖数: 44466
21
我用vc2008的release模式试了一下,编译器没有敢把全局变量换成1...

【在 t****t 的大作中提到】
: of course it will...
t****t
发帖数: 6806
22
probably not precise. i meant objects with external linkage will not be opti
mized away ("global objects" is not a standard usage anway), since compiler
has no idea whether it will be referenced in other translation units. object
s with internal linkage can be optimized away if not referenced in the trans
lation unit, of course. in fact most compiler will warn against this.

【在 P********e 的大作中提到】
: 我同意memory barrier,但如果是register variable呢?
:
: howev
: prob

x****u
发帖数: 44466
23
vc2008的release build。
01171001 mov esi,dword ptr [__imp__printf (11720A0h)]
01171007 push edi
01171008 push offset string "hello!\n" (11720F4h)
0117100D mov dword ptr [global_var (1173374h)],1
01171017 call esi
x = global_var;
01171019 mov edi,dword ptr [global_var (1173374h)]

【在 X****r 的大作中提到】
: int global_var;
: void func() {
: int x;
: gloabl_var = 1;
: x = global_var;
: }
: 你说上面的例子里编译器不会把x = global_var优化成x = 1?

t****t
发帖数: 6806
24
did you compile with MT mode?

【在 x****u 的大作中提到】
: vc2008的release build。
: 01171001 mov esi,dword ptr [__imp__printf (11720A0h)]
: 01171007 push edi
: 01171008 push offset string "hello!\n" (11720F4h)
: 0117100D mov dword ptr [global_var (1173374h)],1
: 01171017 call esi
: x = global_var;
: 01171019 mov edi,dword ptr [global_var (1173374h)]

t****t
发帖数: 6806
25
register automatically means auto, of course. there is no "register global"
objects.
you are not asking valid questions.

【在 P********e 的大作中提到】
: 我同意memory barrier,但如果是register variable呢?
:
: howev
: prob

x****u
发帖数: 44466
26
MD是默认。Multi-threaded DLL (/MD)
这个没关系,MSDN上的文档说MT,MD仅仅是连接不同的库。
/MT
Causes your application to use the multithread, static version of the run-
time library. Defines _MT and causes the compiler to place the library name
LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to
resolve external symbols.
/MD
Causes your application to use the multithread- and DLL-specific version of
the run-time library. Defines _MT and _DLL and causes the compiler to place
the library name MSVCRT.lib i

【在 t****t 的大作中提到】
: did you compile with MT mode?
t****t
发帖数: 6806
27
.type _Z4funcv, @function
_Z4funcv:
.LFB0:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
movl $1, global_var(%rip)
ret
this is what i got from gcc.

【在 x****u 的大作中提到】
: MD是默认。Multi-threaded DLL (/MD)
: 这个没关系,MSDN上的文档说MT,MD仅仅是连接不同的库。
: /MT
: Causes your application to use the multithread, static version of the run-
: time library. Defines _MT and causes the compiler to place the library name
: LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to
: resolve external symbols.
: /MD
: Causes your application to use the multithread- and DLL-specific version of
: the run-time library. Defines _MT and _DLL and causes the compiler to place

t****t
发帖数: 6806
28
wait, why did you call printf before x=global_var?

【在 x****u 的大作中提到】
: vc2008的release build。
: 01171001 mov esi,dword ptr [__imp__printf (11720A0h)]
: 01171007 push edi
: 01171008 push offset string "hello!\n" (11720F4h)
: 0117100D mov dword ptr [global_var (1173374h)],1
: 01171017 call esi
: x = global_var;
: 01171019 mov edi,dword ptr [global_var (1173374h)]

x****u
发帖数: 44466
29
这个例子得做点修改,因为如果x没被引用的话,int x会被优化掉,然后你等于什么都
没写。

【在 t****t 的大作中提到】
: wait, why did you call printf before x=global_var?
X****r
发帖数: 3557
30
gcc -O3 (gcc 4.2.4)
_Z4funcv:
.LFB3:
movl $1, %eax
movl $1, global_var(%rip)
ret
(I added 'return x;' to avoid x being optimized away entirely)

【在 x****u 的大作中提到】
: vc2008的release build。
: 01171001 mov esi,dword ptr [__imp__printf (11720A0h)]
: 01171007 push edi
: 01171008 push offset string "hello!\n" (11720F4h)
: 0117100D mov dword ptr [global_var (1173374h)],1
: 01171017 call esi
: x = global_var;
: 01171019 mov edi,dword ptr [global_var (1173374h)]

相关主题
怎样提高C#计算程序的performance?多线程的程序设计有什么好书推荐? (转载)
推荐一下C++多线程的书吧关于多线程锁:锁代码还是锁资源?
看了这篇文章,脑子有点不够用了c++下如何实现多线程?
进入Programming版参与讨论
x****u
发帖数: 44466
31
我是在后面加了个调用,防止x被优化掉。
未被引用局部变量是必被干掉的,所以我的那个例子里面才体现了加volatile与不加的
区别。

【在 t****t 的大作中提到】
: wait, why did you call printf before x=global_var?
x****u
发帖数: 44466
32
我猜,你再做几个函数虚假给global赋值,情况就不一样了。

【在 X****r 的大作中提到】
: gcc -O3 (gcc 4.2.4)
: _Z4funcv:
: .LFB3:
: movl $1, %eax
: movl $1, global_var(%rip)
: ret
: (I added 'return x;' to avoid x being optimized away entirely)

X****r
发帖数: 3557
33
ft, you called printf("hello!", x), which prevented the global_var
being optimized!

【在 x****u 的大作中提到】
: vc2008的release build。
: 01171001 mov esi,dword ptr [__imp__printf (11720A0h)]
: 01171007 push edi
: 01171008 push offset string "hello!\n" (11720F4h)
: 0117100D mov dword ptr [global_var (1173374h)],1
: 01171017 call esi
: x = global_var;
: 01171019 mov edi,dword ptr [global_var (1173374h)]

x****u
发帖数: 44466
34
我和你一样是在return前面加了个printf,前面的printf没引用x。

ft, you called printf("hello!", x), which prevented the global_var
being optimized!

【在 X****r 的大作中提到】
: ft, you called printf("hello!", x), which prevented the global_var
: being optimized!

t****t
发帖数: 6806
35
what's the point then? if you make it complex such that compiler can't optim
ize it, that doesn't prove your theory of "compiler不敢优化", right?

【在 x****u 的大作中提到】
: 我猜,你再做几个函数虚假给global赋值,情况就不一样了。
P********e
发帖数: 2610
36
all variables are auto by default in c++
register and auto are different ideas.
There are global register variables:
http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html

"

【在 t****t 的大作中提到】
: register automatically means auto, of course. there is no "register global"
: objects.
: you are not asking valid questions.

X****r
发帖数: 3557
37
int global_var;
int func() {
int x;
global_var = 1;
x = global_var;
return x;
}
int func2() {
global_var = 2;
return global_var;
}
int main() {
return func2();
}
assembly:
_Z4funcv:
movl $1, %eax
movl $1, global_var(%rip)
ret
_Z5func2v:
movl $2, %eax
movl $2, global_var(%rip)
ret
main:
movl $2, %eax
movl $2, global_var(%rip)
ret

【在 x****u 的大作中提到】
: 我猜,你再做几个函数虚假给global赋值,情况就不一样了。
t****t
发帖数: 6806
38
see, you totally have no clue. all variables are auto by default? someone tr
ansfer it to joke board.

【在 P********e 的大作中提到】
: all variables are auto by default in c++
: register and auto are different ideas.
: There are global register variables:
: http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html
:
: "

x****u
发帖数: 44466
39
比如我还有几个线程也在操作global,那么这个优化就严重影响语义了。两个线程一读
一写一个global的int的话,锁是不必要的。
gcc优化掉赋值也许是因为它发现global没被其它程序所引用。

optim

【在 t****t 的大作中提到】
: what's the point then? if you make it complex such that compiler can't optim
: ize it, that doesn't prove your theory of "compiler不敢优化", right?

x****u
发帖数: 44466
40
一样啊,func没被调用。你改个两个线程一读一写这个变量,看看gcc结果如何。

【在 X****r 的大作中提到】
: int global_var;
: int func() {
: int x;
: global_var = 1;
: x = global_var;
: return x;
: }
: int func2() {
: global_var = 2;
: return global_var;

相关主题
c++下如何实现多线程?C 多线程的一个问题
有关objec access path的问题请教一个c语言实现多线程的问题
问个C++编译器如何处理函数内的static 变量关于多线程编程的一个问题
进入Programming版参与讨论
X****r
发帖数: 3557
41
ft, 'auto' mean local variable...

【在 P********e 的大作中提到】
: all variables are auto by default in c++
: register and auto are different ideas.
: There are global register variables:
: http://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html
:
: "

t****t
发帖数: 6806
42
of course this optimization will affect semantics. that's why c++0x want to
introduce atomic objects.

【在 x****u 的大作中提到】
: 比如我还有几个线程也在操作global,那么这个优化就严重影响语义了。两个线程一读
: 一写一个global的int的话,锁是不必要的。
: gcc优化掉赋值也许是因为它发现global没被其它程序所引用。
:
: optim

P********e
发帖数: 2610
43
好吧,我刚才说的不准确,应该是local variable are automatically "auto"
不过也绝对不是thrust说的auto = register

【在 X****r 的大作中提到】
: ft, 'auto' mean local variable...
t****t
发帖数: 6806
44
just go get a introductory c book and read it. we adults are discussing seri
ous stuff and you little kid keep silent.

【在 P********e 的大作中提到】
: 好吧,我刚才说的不准确,应该是local variable are automatically "auto"
: 不过也绝对不是thrust说的auto = register

x****u
发帖数: 44466
45
你举个例子看我们说的是不是一回事。

to

【在 t****t 的大作中提到】
: of course this optimization will affect semantics. that's why c++0x want to
: introduce atomic objects.

X****r
发帖数: 3557
46
好,我在main里加一句func();汇编出来的结果还是一样的。
我说的是在没有opaque函数调用的情况下编译器有可能优化全局变量,你说不可能,
我举出实际例子来了,你现在在说什么呢。

【在 x****u 的大作中提到】
: 一样啊,func没被调用。你改个两个线程一读一写这个变量,看看gcc结果如何。
x****u
发帖数: 44466
47
我的原话是"不敢随便优化",因为"可能改变语义"。
你给我举个例子是只引用一次的全局变量,这种东西优化成常数也是可以的。。。你要
是想验证不妨写个多线程竞争的,然后如果gcc还是这么搞我就提交bug去。

【在 X****r 的大作中提到】
: 好,我在main里加一句func();汇编出来的结果还是一样的。
: 我说的是在没有opaque函数调用的情况下编译器有可能优化全局变量,你说不可能,
: 我举出实际例子来了,你现在在说什么呢。

t****t
发帖数: 6806
48
here i used a sleep() to make sure global_var=2 happen after global_var=1. i
should use more delicate mechanism but you get the idea.
int global_var;
void thread1()
{
int x;
global_var=1;
for (int i=0; i<10000000; i++) {
x=global_var;
/* nothing involves global_var */
}
printf("%d", x);
}
void thread2()
{
global_var=2;
}
int main()
{
invoke_thread1();
sleep(3);
invoke_thread2();
}

【在 x****u 的大作中提到】
: 你举个例子看我们说的是不是一回事。
:
: to

x****u
发帖数: 44466
49
你搞两个线程竞争的写一个int,gcc会不会自作主张优化掉?

i

【在 t****t 的大作中提到】
: here i used a sleep() to make sure global_var=2 happen after global_var=1. i
: should use more delicate mechanism but you get the idea.
: int global_var;
: void thread1()
: {
: int x;
: global_var=1;
: for (int i=0; i<10000000; i++) {
: x=global_var;
: /* nothing involves global_var */

t****t
发帖数: 6806
50
this is definitely not a bug because c++ compiler is designed that way. comp
iler has no responsibility for your multithread semantics. it only guarantee
your semantics are correct for single-threaded programs.

【在 x****u 的大作中提到】
: 我的原话是"不敢随便优化",因为"可能改变语义"。
: 你给我举个例子是只引用一次的全局变量,这种东西优化成常数也是可以的。。。你要
: 是想验证不妨写个多线程竞争的,然后如果gcc还是这么搞我就提交bug去。

相关主题
关于线程读写全局变量的问题c++多线程的工作面试一般会问哪些问题?
java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?round function in math
c++posix多线程问题请教关于Dword 和 word
进入Programming版参与讨论
t****t
发帖数: 6806
51
definitely if other condition satisfies.

【在 x****u 的大作中提到】
: 你搞两个线程竞争的写一个int,gcc会不会自作主张优化掉?
:
: i

t****t
发帖数: 6806
52
when did i say auto=register? stop throwing shit on me.
i said register means auto, not the other way. wait, do you have any idea ab
out sufficient condition and necessary condition? probably not.

【在 P********e 的大作中提到】
: 好吧,我刚才说的不准确,应该是local variable are automatically "auto"
: 不过也绝对不是thrust说的auto = register

x****u
发帖数: 44466
53
gcc中有相应的例子吗?

comp
guarantee
你要

【在 t****t 的大作中提到】
: this is definitely not a bug because c++ compiler is designed that way. comp
: iler has no responsibility for your multithread semantics. it only guarantee
: your semantics are correct for single-threaded programs.

t****t
发帖数: 6806
54
what kind of example do you want...

【在 x****u 的大作中提到】
: gcc中有相应的例子吗?
:
: comp
: guarantee
: 你要

P********e
发帖数: 2610
55
kao
if there is any shit, it's throwing back yours....
like:

ab

【在 t****t 的大作中提到】
: what kind of example do you want...
x****u
发帖数: 44466
56
双线程竞争写一个全局变量时gcc把它优化成两个的例子。

【在 t****t 的大作中提到】
: what kind of example do you want...
x****u
发帖数: 44466
57
这个仅仅在gcc的非线程安全的优化模式下才有意义。
现在几乎没有这样的程序非要单线程的极端优化,MSVC也是默认不破坏多线程语义。

【在 t****t 的大作中提到】
: definitely if other condition satisfies.
x****u
发帖数: 44466
58
在例子出来前我还是怀疑GCC能否真的为了优化破坏全局变量语义。

【在 x****u 的大作中提到】
: 这个仅仅在gcc的非线程安全的优化模式下才有意义。
: 现在几乎没有这样的程序非要单线程的极端优化,MSVC也是默认不破坏多线程语义。

t****t
发帖数: 6806
59
you have seen my example. what prevents me from adding another translation
unit and add another thread?
pls note i didn't compile to executable. i just compile to .o.

【在 x****u 的大作中提到】
: 在例子出来前我还是怀疑GCC能否真的为了优化破坏全局变量语义。
x****u
发帖数: 44466
60
你那个不是多线程的,语义没被破坏。

translation

【在 t****t 的大作中提到】
: you have seen my example. what prevents me from adding another translation
: unit and add another thread?
: pls note i didn't compile to executable. i just compile to .o.

相关主题
多线程编程前景如何?推荐一下C++多线程的书吧
多线程 编程,process 和 thread 的一些问题。看了这篇文章,脑子有点不够用了
怎样提高C#计算程序的performance?多线程的程序设计有什么好书推荐? (转载)
进入Programming版参与讨论
t****t
发帖数: 6806
61
...did you read my post? i said, nothing prevents me from adding another thr
ead in another translation unit. so it IS a multithreaded example. in fact,
compiler does not distinguish between single threaded and multi threaded app
lication at all. at least gcc does not.

【在 x****u 的大作中提到】
: 你那个不是多线程的,语义没被破坏。
:
: translation

t****t
发帖数: 6806
62
actually, have you read the famous paper "Threads can not be implemented as
a library"? It shows locks only "almost" works. that is, even if you use loc
k, things can get quite ugly sometimes. Highly recommended.
i think this directly leads to the new memory model and atomic object concep
t of c++0x.

【在 x****u 的大作中提到】
: 你那个不是多线程的,语义没被破坏。
:
: translation

x****u
发帖数: 44466
63
不写出来甚至不够提交bug的,还是停留在推测阶段。
GCC甚至有了专门的option保证static的多线程安全,而且多线程也已经是最常用的
option之一了,很难想象它会为了优化破坏global的线程安全性。

thr
,
app

【在 t****t 的大作中提到】
: ...did you read my post? i said, nothing prevents me from adding another thr
: ead in another translation unit. so it IS a multithreaded example. in fact,
: compiler does not distinguish between single threaded and multi threaded app
: lication at all. at least gcc does not.

t****t
发帖数: 6806
64
did you mean -pthread? that is the same as /MT for VC. only links with diffe
rent library and defines some macro.
and I repeat! this is not a bug!!!

【在 x****u 的大作中提到】
: 不写出来甚至不够提交bug的,还是停留在推测阶段。
: GCC甚至有了专门的option保证static的多线程安全,而且多线程也已经是最常用的
: option之一了,很难想象它会为了优化破坏global的线程安全性。
:
: thr
: ,
: app

x****u
发帖数: 44466
65
不用这么麻烦,如果gcc一定要猜测全局变量值,直接把它定义成volatile就安全了。

as
loc
concep

【在 t****t 的大作中提到】
: actually, have you read the famous paper "Threads can not be implemented as
: a library"? It shows locks only "almost" works. that is, even if you use loc
: k, things can get quite ugly sometimes. Highly recommended.
: i think this directly leads to the new memory model and atomic object concep
: t of c++0x.

x****u
发帖数: 44466
66
这个问题不涉及第三方library,完全是编译器行为问题。
现在还没有证据说gcc一定会使多线程程序语义破坏吧,谈是不是bug比较早了。

diffe

【在 t****t 的大作中提到】
: did you mean -pthread? that is the same as /MT for VC. only links with diffe
: rent library and defines some macro.
: and I repeat! this is not a bug!!!

t****t
发帖数: 6806
67
yeah, volatile adds another layer of protection. but the semantics of volati
le is quite different from atomic; i can not describe how, since i am still
studying the new atomic thing. one thing i can be sure is volatile does not
add barrier; so it only works for compiler from adding incorrect optimizatio
n, not for the processor from making incorrect out-of-order memory access.

【在 x****u 的大作中提到】
: 不用这么麻烦,如果gcc一定要猜测全局变量值,直接把它定义成volatile就安全了。
:
: as
: loc
: concep

t****t
发帖数: 6806
68
i said, even if gcc "corrupts" your multithreaded semantics, it is still not
a bug.
it is not a bug! it is not a bug!

【在 x****u 的大作中提到】
: 这个问题不涉及第三方library,完全是编译器行为问题。
: 现在还没有证据说gcc一定会使多线程程序语义破坏吧,谈是不是bug比较早了。
:
: diffe

x****u
发帖数: 44466
69
OK。那我就把这个问题简化一下了。
书写C/C++多线程程序时,如果定义了多线程共享的全局变量,是否一定要加上
volatile修饰符以保证gcc不在极端的情况下对其优化。
你认为有必要吗?

volati
still
not
optimizatio

【在 t****t 的大作中提到】
: yeah, volatile adds another layer of protection. but the semantics of volati
: le is quite different from atomic; i can not describe how, since i am still
: studying the new atomic thing. one thing i can be sure is volatile does not
: add barrier; so it only works for compiler from adding incorrect optimizatio
: n, not for the processor from making incorrect out-of-order memory access.

m*****e
发帖数: 4193
70

volatile is neither necessary nor sufficient. Don't use it.

【在 x****u 的大作中提到】
: OK。那我就把这个问题简化一下了。
: 书写C/C++多线程程序时,如果定义了多线程共享的全局变量,是否一定要加上
: volatile修饰符以保证gcc不在极端的情况下对其优化。
: 你认为有必要吗?
:
: volati
: still
: not
: optimizatio

相关主题
多线程的程序设计有什么好书推荐? (转载)有关objec access path的问题
关于多线程锁:锁代码还是锁资源?问个C++编译器如何处理函数内的static 变量
c++下如何实现多线程?C 多线程的一个问题
进入Programming版参与讨论
t****t
发帖数: 6806
71
if you use locking mechanism, most likely not (as xentar said in the beginni
ng); but in rare cases (see the HP paper), bad things may happen although 99
.9% cases it won't.
if you don't, most likely you have to.

【在 x****u 的大作中提到】
: OK。那我就把这个问题简化一下了。
: 书写C/C++多线程程序时,如果定义了多线程共享的全局变量,是否一定要加上
: volatile修饰符以保证gcc不在极端的情况下对其优化。
: 你认为有必要吗?
:
: volati
: still
: not
: optimizatio

x****u
发帖数: 44466
72
你得有根据。
如果global variable被编译器优化成了两个不相干的值,那么这就直接违反了其定义
,必须在every scope里面accessible。
C语言里面从没有说这句话有个单线程的前提,所以还是可以被报告bug。

not

【在 t****t 的大作中提到】
: i said, even if gcc "corrupts" your multithreaded semantics, it is still not
: a bug.
: it is not a bug! it is not a bug!

t****t
发帖数: 6806
73
well, the correct decorator is "atomic". i agree volatile is not sufficie
nt. but see this link for some additional information.
http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/user-faq.html

【在 m*****e 的大作中提到】
:
: volatile is neither necessary nor sufficient. Don't use it.

x****u
发帖数: 44466
74
你这个most likely目前甚至还没有sample。

beginni
99

【在 t****t 的大作中提到】
: if you use locking mechanism, most likely not (as xentar said in the beginni
: ng); but in rare cases (see the HP paper), bad things may happen although 99
: .9% cases it won't.
: if you don't, most likely you have to.

x****u
发帖数: 44466
75
atmoic是volatile的超集,在内核里面所有涉及lock的地方都非常严谨,不能随便碰。

sufficie

【在 t****t 的大作中提到】
: well, the correct decorator is "atomic". i agree volatile is not sufficie
: nt. but see this link for some additional information.
: http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/user-faq.html

t****t
发帖数: 6806
76
what do you mean by "优化成两个不相干的值"? violating what definition?
I am not sure about C; but C++ standard defines a program execution by abstr
act machine. if the program matches "one of the possible result of abstract
machine", then it is conforming. the standard delibrately defined a quite re
laxing abstract machine.if you have to consider this is a bug, go ahead and
by all means report it. well, the limit of current execution model of c/c++
is quite well-known, so most likely no one will take it s

【在 x****u 的大作中提到】
: 你得有根据。
: 如果global variable被编译器优化成了两个不相干的值,那么这就直接违反了其定义
: ,必须在every scope里面accessible。
: C语言里面从没有说这句话有个单线程的前提,所以还是可以被报告bug。
:
: not

t****t
发帖数: 6806
77
come on, i have given the sample. do i have to type it literally? since you
have doubts, not me, you go ahead and implement it.

【在 x****u 的大作中提到】
: 你这个most likely目前甚至还没有sample。
:
: beginni
: 99

x****u
发帖数: 44466
78
我搜了C标准,找不到什么对线程模型的描述。所以这种优化如果对多线程产生问题,
多半是违反C语法。
C++的abstract machine有没有规定必须是单线程的?
咱们讨论的是编译器的行为。我认为gcc很可能根本就不按照你的想法运行,所以most
likely根本就没有bug。
Wikipedia里面的条目也只是含糊的说,优化的前提是编译器发现no other code can
possibly change the value,这里面也没说前提是单线程。

abstr
abstract
re
and
+
j

【在 t****t 的大作中提到】
: what do you mean by "优化成两个不相干的值"? violating what definition?
: I am not sure about C; but C++ standard defines a program execution by abstr
: act machine. if the program matches "one of the possible result of abstract
: machine", then it is conforming. the standard delibrately defined a quite re
: laxing abstract machine.if you have to consider this is a bug, go ahead and
: by all means report it. well, the limit of current execution model of c/c++
: is quite well-known, so most likely no one will take it s

x****u
发帖数: 44466
79
我手头没有gcc的环境,msvc的行为和我预测一致。
另外我能查到的所有文档都没有说global variable仅仅在单线程时是global的,质疑
标准的其实是你。

you

【在 t****t 的大作中提到】
: come on, i have given the sample. do i have to type it literally? since you
: have doubts, not me, you go ahead and implement it.

t****t
发帖数: 6806
80
OK, you win. here's my refined example. comments on next post.
#include
using namespace std;
unsigned int global_var;
void* func(void*) {
unsigned int x=0;
global_var = 1;
for (long i=0; i<10000000000L; i++) {
x=x*2;
x+= global_var;
}
cout< }
void* func1(void*) {
global_var=2;
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, func, NULL);
sleep(3);
pthread_

【在 x****u 的大作中提到】
: 我手头没有gcc的环境,msvc的行为和我预测一致。
: 另外我能查到的所有文档都没有说global variable仅仅在单线程时是global的,质疑
: 标准的其实是你。
:
: you

相关主题
请教一个c语言实现多线程的问题java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
关于多线程编程的一个问题c++posix多线程问题请教
关于线程读写全局变量的问题c++多线程的工作面试一般会问哪些问题?
进入Programming版参与讨论
t****t
发帖数: 6806
81
func() will repeatly make x*=2 and x+=global_var.
if global_var is 1, then the final result should be (unsigned)-1.
if global_var changes to 2 in the middle, then the final result should be (
unsigned)-2. straightforward enough.
func1() will change the global_var to 2.
func() will start first. then after 3 sec, func1() will change global_var to
2. so the final result should be (unsigned)-2.
the length of loop is arbitrary, which makes my computer execute the loop
for more than 3 seconds.
now her

【在 t****t 的大作中提到】
: OK, you win. here's my refined example. comments on next post.
: #include
: using namespace std;
: unsigned int global_var;
: void* func(void*) {
: unsigned int x=0;
: global_var = 1;
: for (long i=0; i<10000000000L; i++) {
: x=x*2;
: x+= global_var;

t****t
发帖数: 6806
82
c++ abstract machine says "[1.8] 8 Once the execution of a function begins,
no expressions from the calling function are evaluated until execution of
the called function has completed.8)"
and footnote 8 says "8) In other words, function executions do not
interleave with each other."
I think this is clear enough.

most

【在 x****u 的大作中提到】
: 我搜了C标准,找不到什么对线程模型的描述。所以这种优化如果对多线程产生问题,
: 多半是违反C语法。
: C++的abstract machine有没有规定必须是单线程的?
: 咱们讨论的是编译器的行为。我认为gcc很可能根本就不按照你的想法运行,所以most
: likely根本就没有bug。
: Wikipedia里面的条目也只是含糊的说,优化的前提是编译器发现no other code can
: possibly change the value,这里面也没说前提是单线程。
:
: abstr
: abstract

x****u
发帖数: 44466
83
你把汇编贴上来看看?

to

【在 t****t 的大作中提到】
: func() will repeatly make x*=2 and x+=global_var.
: if global_var is 1, then the final result should be (unsigned)-1.
: if global_var changes to 2 in the middle, then the final result should be (
: unsigned)-2. straightforward enough.
: func1() will change the global_var to 2.
: func() will start first. then after 3 sec, func1() will change global_var to
: 2. so the final result should be (unsigned)-2.
: the length of loop is arbitrary, which makes my computer execute the loop
: for more than 3 seconds.
: now her

t****t
发帖数: 6806
84
subq $8, %rsp
.cfi_def_cfa_offset 32
movl $1, global_var(%rip)
.p2align 4,,10
.p2align 3
.L8:
addq $1, %rax
leal 1(%rdx,%rdx), %edx
cmpq $1000000000, %rax
jne .L8
clear enough?

【在 x****u 的大作中提到】
: 你把汇编贴上来看看?
:
: to

x****u
发帖数: 44466
85
你这个程序是C的吧,C语法定义有这一条吗?另外把汇编贴上来比较说明问题。

,

【在 t****t 的大作中提到】
: c++ abstract machine says "[1.8] 8 Once the execution of a function begins,
: no expressions from the calling function are evaluated until execution of
: the called function has completed.8)"
: and footnote 8 says "8) In other words, function executions do not
: interleave with each other."
: I think this is clear enough.
:
: most

t****t
发帖数: 6806
86
...you are joking with me right? i used iostream if you care.

【在 x****u 的大作中提到】
: 你这个程序是C的吧,C语法定义有这一条吗?另外把汇编贴上来比较说明问题。
:
: ,

x****u
发帖数: 44466
87
OK,VC编译器无法编译无返回值的函数。

【在 t****t 的大作中提到】
: ...you are joking with me right? i used iostream if you care.
t****t
发帖数: 6806
88
go ahead and add a return null for me then. for god's sake, it's an informal
example, not a formal proof. you got the idea don't you?

【在 x****u 的大作中提到】
: OK,VC编译器无法编译无返回值的函数。
x****u
发帖数: 44466
89
你不要太激动,我也是在研究这个细节。
我在VC上编译你这个程序得到的汇编也一样,说明这个行为普遍存在,不管符合不符合
语法,在多线程的全局变量前一定要加上volatile防止编译器自作主张cache值。
这个是没有volatile的
00321002 in al,dx
00321003 and esp,0FFFFFFF8h
unsigned int x=0;
00321006 xor eax,eax
global_var = 1;
for (long long i=0; i<10000000000L; i++) {
00321008 xor edx,edx
0032100A mov dword ptr [global_var (323378h)],1
00321014 xor ecx,ecx
00321016 add edx,1
00321019 adc ecx,0
0032101C cmp

【在 t****t 的大作中提到】
: go ahead and add a return null for me then. for god's sake, it's an informal
: example, not a formal proof. you got the idea don't you?

m*****e
发帖数: 4193
90

Or use proper locking. Geez.

【在 x****u 的大作中提到】
: 你不要太激动,我也是在研究这个细节。
: 我在VC上编译你这个程序得到的汇编也一样,说明这个行为普遍存在,不管符合不符合
: 语法,在多线程的全局变量前一定要加上volatile防止编译器自作主张cache值。
: 这个是没有volatile的
: 00321002 in al,dx
: 00321003 and esp,0FFFFFFF8h
: unsigned int x=0;
: 00321006 xor eax,eax
: global_var = 1;
: for (long long i=0; i<10000000000L; i++) {

相关主题
round function in math多线程 编程,process 和 thread 的一些问题。
关于Dword 和 word怎样提高C#计算程序的performance?
多线程编程前景如何?推荐一下C++多线程的书吧
进入Programming版参与讨论
x****u
发帖数: 44466
91
lock和volatile不等价,有的地方用不了lock。

【在 m*****e 的大作中提到】
:
: Or use proper locking. Geez.

x****u
发帖数: 44466
92
测试用的VC版程序:
#include
#include
/*volatile */unsigned int global_var;
DWORD WINAPI func(void* arg) {
unsigned int x=0;
long long i;
global_var = 1;
for (i=0; i<10000000000L; i++) {
x=x*2;
x+= global_var;
}
printf("%d\n", x);
return 0;
}
DWORD WINAPI func1(void* arg) {
global_var=2;
return 0;
}
int main()
{
HANDLE t1, t2;
t1 = C

【在 x****u 的大作中提到】
: 你不要太激动,我也是在研究这个细节。
: 我在VC上编译你这个程序得到的汇编也一样,说明这个行为普遍存在,不管符合不符合
: 语法,在多线程的全局变量前一定要加上volatile防止编译器自作主张cache值。
: 这个是没有volatile的
: 00321002 in al,dx
: 00321003 and esp,0FFFFFFF8h
: unsigned int x=0;
: 00321006 xor eax,eax
: global_var = 1;
: for (long long i=0; i<10000000000L; i++) {

t****t
发帖数: 6806
93
我早跟你说了没什么可研究的, 这个是well defined behaviour and well known, C/C
++本来就是单线程模型, 你非要当个大发现还硬说是bug. 同步本来就应该靠intrinsic
, 或者一定要lock-free的话, 用atomic. volatile就是在没有atomic的情况下一个临
时的代替品, 自己也不是很靠谱的, 何况还有CPU在那里搞乱序.

【在 x****u 的大作中提到】
: 你不要太激动,我也是在研究这个细节。
: 我在VC上编译你这个程序得到的汇编也一样,说明这个行为普遍存在,不管符合不符合
: 语法,在多线程的全局变量前一定要加上volatile防止编译器自作主张cache值。
: 这个是没有volatile的
: 00321002 in al,dx
: 00321003 and esp,0FFFFFFF8h
: unsigned int x=0;
: 00321006 xor eax,eax
: global_var = 1;
: for (long long i=0; i<10000000000L; i++) {

t****t
发帖数: 6806
94
贴它干嘛, xxsl.

【在 x****u 的大作中提到】
: 测试用的VC版程序:
: #include
: #include
: /*volatile */unsigned int global_var;
: DWORD WINAPI func(void* arg) {
: unsigned int x=0;
: long long i;
: global_var = 1;
: for (i=0; i<10000000000L; i++) {
: x=x*2;

t****t
发帖数: 6806
95
man, "register global" you posted is a gcc extension. you have to specify
which register explicitly AND it clearly says "not safe to access from more
than one thread".
i said you have no clue. look at your old posts, it's quite difficult to
find out a completely correct one. it is like this several years ago, and it
is like this today. you have no clue, and you are staying at the status of
cluelessness. plus you can't speak chinese properly.

【在 P********e 的大作中提到】
: kao
: if there is any shit, it's throwing back yours....
: like:
:
: ab

x****u
发帖数: 44466
96
一般的说法是C不保证线程安全,关于C优化还可以破坏多线程global语义,而且至今也
没找到明确依据(你那个是C++的),这个语法中没有明确定义的东西多了去了。
乱序是另一个话题了。。。用atomic代替volatile是不行的,锁不能乱用。

/C
intrinsic

【在 t****t 的大作中提到】
: 我早跟你说了没什么可研究的, 这个是well defined behaviour and well known, C/C
: ++本来就是单线程模型, 你非要当个大发现还硬说是bug. 同步本来就应该靠intrinsic
: , 或者一定要lock-free的话, 用atomic. volatile就是在没有atomic的情况下一个临
: 时的代替品, 自己也不是很靠谱的, 何况还有CPU在那里搞乱序.

X****r
发帖数: 3557
97
如果有mutex保护,如楼主原帖所说的情况,不需要加volatile,原因
thrust已经讲得很清楚了(opaque function)。
如果没有mutex(或者其他同步机制)保护,需要加volatile,也不能保证对,
原因thrust也已经讲得很清楚了(不保证atomic,也不是memory barrier),
所以这种情况基本上不存在(加了volatile也基本没用)。

【在 x****u 的大作中提到】
: 你不要太激动,我也是在研究这个细节。
: 我在VC上编译你这个程序得到的汇编也一样,说明这个行为普遍存在,不管符合不符合
: 语法,在多线程的全局变量前一定要加上volatile防止编译器自作主张cache值。
: 这个是没有volatile的
: 00321002 in al,dx
: 00321003 and esp,0FFFFFFF8h
: unsigned int x=0;
: 00321006 xor eax,eax
: global_var = 1;
: for (long long i=0; i<10000000000L; i++) {

x****u
发帖数: 44466
98
没人说任何情况加volatile保证对,但本程序加上了就是保证对。
因为这个线程必须是单一函数,不可能被优化成内联。func1里面就一条语句,所以不
用担心被编译器或者CPU乱序掉。
我从本楼的开始就强调锁和volatile是两回事,这个优化是否改变global的行为是半路
插过来的,不是我举的例子。要是什么全局变量都上atomic,还不如用java了。

【在 X****r 的大作中提到】
: 如果有mutex保护,如楼主原帖所说的情况,不需要加volatile,原因
: thrust已经讲得很清楚了(opaque function)。
: 如果没有mutex(或者其他同步机制)保护,需要加volatile,也不能保证对,
: 原因thrust也已经讲得很清楚了(不保证atomic,也不是memory barrier),
: 所以这种情况基本上不存在(加了volatile也基本没用)。

z****e
发帖数: 2024
99
我靠,我一天不来,就乱成这样了???
还没来得及看全回帖。
1. mutex 就够了。mutex就是干lz说的这个情况用的。
2. C++根本就是单线程模式,其多线程依赖于具体的library。C++0x已经在标准里边加
了多线程,std::thread. gcc 4.4以上可用,windows下用just::thread,这个是C++的
未来。其他的库都不是标准支持的,而多线程从理论上说,是不能用库来实现的。有大
牛的一篇文章讲这个。
e*l
发帖数: 37
100
C89标准里就已经有volatile了,C++98标准只不过是接收而已,难道还能搞成跟C不兼
容?
如果使用了锁,也就确保了在加锁这段区间,共享变量不会被其他线程修改,那么加
volatile多余,反而阻止编译器在这段区间做优化。

【在 x****u 的大作中提到】
: 一般的说法是C不保证线程安全,关于C优化还可以破坏多线程global语义,而且至今也
: 没找到明确依据(你那个是C++的),这个语法中没有明确定义的东西多了去了。
: 乱序是另一个话题了。。。用atomic代替volatile是不行的,锁不能乱用。
:
: /C
: intrinsic

相关主题
看了这篇文章,脑子有点不够用了c++下如何实现多线程?
多线程的程序设计有什么好书推荐? (转载)有关objec access path的问题
关于多线程锁:锁代码还是锁资源?问个C++编译器如何处理函数内的static 变量
进入Programming版参与讨论
p***o
发帖数: 1252
101
单线程语义 is well defined.
There is no such thing called 多线程语义, especially for multiple variables.

【在 x****u 的大作中提到】
: 这个仅仅在gcc的非线程安全的优化模式下才有意义。
: 现在几乎没有这样的程序非要单线程的极端优化,MSVC也是默认不破坏多线程语义。

p***o
发帖数: 1252
102
The behavior of a single variable in a multi-threaded program matches the
usual expectation of programmers as you since it doesn't hurt performance
a lot in processors through cache coherence protocols.
The behavior of a group of variables in such a program doesn't match your
expectation, usually known as sequential consistency, because of performance
reasons. Search "memory consistency model" for details.
Current C/C++ standard doesn't consider this issue. Implementations are
rather compiler-de

【在 x****u 的大作中提到】
: 我搜了C标准,找不到什么对线程模型的描述。所以这种优化如果对多线程产生问题,
: 多半是违反C语法。
: C++的abstract machine有没有规定必须是单线程的?
: 咱们讨论的是编译器的行为。我认为gcc很可能根本就不按照你的想法运行,所以most
: likely根本就没有bug。
: Wikipedia里面的条目也只是含糊的说,优化的前提是编译器发现no other code can
: possibly change the value,这里面也没说前提是单线程。
:
: abstr
: abstract

r****o
发帖数: 1950
103
多谢,看来这个说法应该是正解了,呵呵。

【在 X****r 的大作中提到】
: 如果有mutex保护,如楼主原帖所说的情况,不需要加volatile,原因
: thrust已经讲得很清楚了(opaque function)。
: 如果没有mutex(或者其他同步机制)保护,需要加volatile,也不能保证对,
: 原因thrust也已经讲得很清楚了(不保证atomic,也不是memory barrier),
: 所以这种情况基本上不存在(加了volatile也基本没用)。

e*l
发帖数: 37
104
他说得应该是并行模型
有些语言从语义上就支持,比如erlang.
现在新发明的语言或多或少都会从语言层面支持这个.

variables.

【在 p***o 的大作中提到】
: 单线程语义 is well defined.
: There is no such thing called 多线程语义, especially for multiple variables.

e*l
发帖数: 37
105
c/c++标准中都对volatile有明确规定,怎么能说c/c++没有考虑内存一致性? 应该说c/c
++没有考虑线程安全,认为这个应该由用户考虑.
至于vc编译器实现volatile的语义前后有差异,这个只能说vc没有完全支持标准,不能把
责任推给c/c++标准.
java一样也并没有从语言本身支持并行模型, 并不见得有多高明, 可以对比erlang, 以
及一些新出的语言.

performance
题,
can

【在 p***o 的大作中提到】
: The behavior of a single variable in a multi-threaded program matches the
: usual expectation of programmers as you since it doesn't hurt performance
: a lot in processors through cache coherence protocols.
: The behavior of a group of variables in such a program doesn't match your
: expectation, usually known as sequential consistency, because of performance
: reasons. Search "memory consistency model" for details.
: Current C/C++ standard doesn't consider this issue. Implementations are
: rather compiler-de

p***o
发帖数: 1252
106
So which part of the C++ standard defines the memory consistency model?
I don't think it's defined in the 1998 version of the standard.

/c

【在 e*l 的大作中提到】
: c/c++标准中都对volatile有明确规定,怎么能说c/c++没有考虑内存一致性? 应该说c/c
: ++没有考虑线程安全,认为这个应该由用户考虑.
: 至于vc编译器实现volatile的语义前后有差异,这个只能说vc没有完全支持标准,不能把
: 责任推给c/c++标准.
: java一样也并没有从语言本身支持并行模型, 并不见得有多高明, 可以对比erlang, 以
: 及一些新出的语言.
:
: performance
: 题,
: can

p***o
发帖数: 1252
107
I assume you are talking about the model to create threads,
e.g. explicitly as fork/join or implicitly as data flow.
But that has nothing to do with memory consistency model.
This is something new. Even the advanced textbook on computer
architecture won't discuss it in detail.

【在 e*l 的大作中提到】
: 他说得应该是并行模型
: 有些语言从语义上就支持,比如erlang.
: 现在新发明的语言或多或少都会从语言层面支持这个.
:
: variables.

O*******d
发帖数: 20343
108
volatile不是为了保护,而是为了不用cache,防止一个变量有两个copy。

全局

【在 r****o 的大作中提到】
: 请问,在multithreading环境下,假如用了mutex来保护某个全局变量,是不是对该全局
: 变量仍然要加volatile关键字才能保证该变量的值是没有被其他线程改过的?

X****r
发帖数: 3557
109
More precisely, 'volatile' makes accessing an object a
side-effect. Side-effect is important because it defines
the behavior of the abstract machine -- at sequence points,
all side effects of previous evaluations shall be complete
and no side effects of subsequent evaluations shall have
taken place.
The original intent of the volatile keyword had nothing
to do with concurrent programming. It was for memory-mapped
I/O, and also for execution flows that the compiler is
unaware of, such as long jum

【在 O*******d 的大作中提到】
: volatile不是为了保护,而是为了不用cache,防止一个变量有两个copy。
:
: 全局

e*l
发帖数: 37
110
memory consistency model并不是新东西,我们这里谈到的volatile相关的话题跟
shared memory distribution system没啥关系。
唯一的问题就是在单机上(不管多cpu或单cpu多核系统),虽然内存一致性是严格满足
的,但是由于在缺少volatile的指示,使得编译器优化时引入不一致的可能。
如果你说的是底层cpu之间或者核之间的内存一致性问题,那就跟这个问题更不相关了
,这个是由于cache的存在导致的,是设计cpu架构(硬件)的人应该考虑和解决的。
这里编程讨论大多集中在并行编程问题,大家不太可能去深究cpu系统架构甚至硬件。

【在 p***o 的大作中提到】
: I assume you are talking about the model to create threads,
: e.g. explicitly as fork/join or implicitly as data flow.
: But that has nothing to do with memory consistency model.
: This is something new. Even the advanced textbook on computer
: architecture won't discuss it in detail.

相关主题
问个C++编译器如何处理函数内的static 变量关于多线程编程的一个问题
C 多线程的一个问题关于线程读写全局变量的问题
请教一个c语言实现多线程的问题java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
进入Programming版参与讨论
e*l
发帖数: 37
111
单机上内存一直性是严格满足的(分布式系统比较困难),cpu硬件架构从底层就已经
支持
但是具体到编程,编译器的优化可能破坏一致性,所以c/c++里引入了volatile确保了
一致性。

【在 p***o 的大作中提到】
: So which part of the C++ standard defines the memory consistency model?
: I don't think it's defined in the 1998 version of the standard.
:
: /c

e*l
发帖数: 37
112
cache, copy容易让人误会
内联汇编里有一个限定符memory,比如__asm__("":::"memory");起到的作用跟
volatile修饰差不多,强迫从内存访问,而不是寄存器。
编译器优化时,如果可能,会把变量值从内存直接存入寄存器,以后所有操作都是针对
该寄存器,即使寄存器值改变,也不会立刻写回内存,而在此期间其它线程也可能修改
该变量的内存值或者读取该变量值,从而导致不一致。

【在 O*******d 的大作中提到】
: volatile不是为了保护,而是为了不用cache,防止一个变量有两个copy。
:
: 全局

O*******d
发帖数: 20343
113
多谢从理论上解答。 从实际应用上来理解,就是volatile变量每次都要从内存
读取,赋值后要写入内存。 这对多线程的程序比较重要。

【在 X****r 的大作中提到】
: More precisely, 'volatile' makes accessing an object a
: side-effect. Side-effect is important because it defines
: the behavior of the abstract machine -- at sequence points,
: all side effects of previous evaluations shall be complete
: and no side effects of subsequent evaluations shall have
: taken place.
: The original intent of the volatile keyword had nothing
: to do with concurrent programming. It was for memory-mapped
: I/O, and also for execution flows that the compiler is
: unaware of, such as long jum

X****r
发帖数: 3557
114
前面已经说过了,volatile对多线程基本上没有帮助……
这篇blog有详细的解释:
http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-
for-multi-threaded-programming/

【在 O*******d 的大作中提到】
: 多谢从理论上解答。 从实际应用上来理解,就是volatile变量每次都要从内存
: 读取,赋值后要写入内存。 这对多线程的程序比较重要。

c*****t
发帖数: 1879
115
I remember that microbe and I argued this "volatile" keyword a few years
ago.
For compilers, they could easily optimize the calling of known library
functions (such as C library functions). These functions would not modify
global variables unknown to the compiler. Also, extern functions could not
access static variables.
1. In these cases, the access of the variables could easily be reordered
around the function calls.
2. The variable might be cached. For the obvious reason, variables could
b
p***o
发帖数: 1252
116
OK,我想我知道你那里不明白了。关于内存一致性,有两个层面。一是
cache coherence,这个保证任何一个内存位置在同一个系统中看起来
是一样的。这一条符合大多数人的直观想法,也确实是严格满足的。
第二个是memory consistency, 这个和不同内存位置的读写顺序有关。
大多数人的直观想法是这些读写存在一个全局的顺序,或者叫sequential
consistency。如果单机只有一个核,这没有问题,因为不管你有多少个
线程,每条指令是atomic的并且指令只能交错运行。问题是现在的单机
有多个核,你要实现sequential consistency的话对于系统速度影响
太大,这是不可接受的。所以你能买到的多核/多CPU系统全部--不满足--
sequential consistency, 也就是你直观上的想法。
至于它们满足什么memory consistency model,你只能去查手册,比如
Intel? 64 and IA-32 Architectures Software Developer’s Manual
Volume 3A: System Progr

【在 e*l 的大作中提到】
: 单机上内存一直性是严格满足的(分布式系统比较困难),cpu硬件架构从底层就已经
: 支持
: 但是具体到编程,编译器的优化可能破坏一致性,所以c/c++里引入了volatile确保了
: 一致性。

e******d
发帖数: 310
117
Volatile keyword tells CPU to fetch data from main memory when it needs data
even though the data is present in L1/L2 cache. Volatile keyword is
particularly useful in developing hardware driver.
t****t
发帖数: 6806
118
this is not guaranteed so it is implementation defined.
for one thing, there is no "cache" concept appearing anywhere in the C/C++
standard.

data

【在 e******d 的大作中提到】
: Volatile keyword tells CPU to fetch data from main memory when it needs data
: even though the data is present in L1/L2 cache. Volatile keyword is
: particularly useful in developing hardware driver.

X****r
发帖数: 3557
119
Are you sure you are talking about the keyword in C/C++?
If so this is plain wrong. The keyword does nothing like that. I hope
you did not write any code under such assumption.
By the way, I once read somewhere that a majority of Windows crashes
are caused by device drivers. Maybe this is just unrelated...

data

【在 e******d 的大作中提到】
: Volatile keyword tells CPU to fetch data from main memory when it needs data
: even though the data is present in L1/L2 cache. Volatile keyword is
: particularly useful in developing hardware driver.

p***o
发帖数: 1252
120

Is there any other way to crash Windows?

【在 X****r 的大作中提到】
: Are you sure you are talking about the keyword in C/C++?
: If so this is plain wrong. The keyword does nothing like that. I hope
: you did not write any code under such assumption.
: By the way, I once read somewhere that a majority of Windows crashes
: are caused by device drivers. Maybe this is just unrelated...
:
: data

相关主题
c++posix多线程问题请教关于Dword 和 word
c++多线程的工作面试一般会问哪些问题?多线程编程前景如何?
round function in math多线程 编程,process 和 thread 的一些问题。
进入Programming版参与讨论
e*l
发帖数: 37
121
所以说在这里谈这种底层的consistency与讨论的话题关系不大
由于一致性这个词在不同的层面有不同的含义, 我只是不确定你说的是哪一层,所以我
只能猜了.
对于多核/多cpu系统,一致性也应该满足,比如写后读,一核修改了cache,会给其它核发
消息告知该cache对应的内存已经变化,其它核读时会从内存更新自己的cache行. 至于
多个核同时修改同一段内存对应的cache,哪一个核会先更新该内存,cpu有自己的策略,
由于同时性这个不能用一致性来评价, 而且更重要的这个应该由应用程序来确保,cpu没
法也没必要做保证,因此并行模型的同步机制才显得很重要.

已经
保了

【在 p***o 的大作中提到】
: OK,我想我知道你那里不明白了。关于内存一致性,有两个层面。一是
: cache coherence,这个保证任何一个内存位置在同一个系统中看起来
: 是一样的。这一条符合大多数人的直观想法,也确实是严格满足的。
: 第二个是memory consistency, 这个和不同内存位置的读写顺序有关。
: 大多数人的直观想法是这些读写存在一个全局的顺序,或者叫sequential
: consistency。如果单机只有一个核,这没有问题,因为不管你有多少个
: 线程,每条指令是atomic的并且指令只能交错运行。问题是现在的单机
: 有多个核,你要实现sequential consistency的话对于系统速度影响
: 太大,这是不可接受的。所以你能买到的多核/多CPU系统全部--不满足--
: sequential consistency, 也就是你直观上的想法。

x****u
发帖数: 44466
122
他说的实际上是CPU乱序执行对多线程产生的问题。
比如有如下两个指令,如果你假定B为2的时候A一定为1,就可能产生问题,因为另一个
CPU可能对其乱序执行,B在A前被赋值。
A=1
B=2

【在 e*l 的大作中提到】
: 所以说在这里谈这种底层的consistency与讨论的话题关系不大
: 由于一致性这个词在不同的层面有不同的含义, 我只是不确定你说的是哪一层,所以我
: 只能猜了.
: 对于多核/多cpu系统,一致性也应该满足,比如写后读,一核修改了cache,会给其它核发
: 消息告知该cache对应的内存已经变化,其它核读时会从内存更新自己的cache行. 至于
: 多个核同时修改同一段内存对应的cache,哪一个核会先更新该内存,cpu有自己的策略,
: 由于同时性这个不能用一致性来评价, 而且更重要的这个应该由应用程序来确保,cpu没
: 法也没必要做保证,因此并行模型的同步机制才显得很重要.
:
: 已经

p***o
发帖数: 1252
123
The truth is languages like C and C++ don't hide such 底层的 consistency
from you. It's OK if you just use libraries. Anything beyond existing
libraries like lock-free constructs that lead to very efficient code,
requires such knowledge.

【在 e*l 的大作中提到】
: 所以说在这里谈这种底层的consistency与讨论的话题关系不大
: 由于一致性这个词在不同的层面有不同的含义, 我只是不确定你说的是哪一层,所以我
: 只能猜了.
: 对于多核/多cpu系统,一致性也应该满足,比如写后读,一核修改了cache,会给其它核发
: 消息告知该cache对应的内存已经变化,其它核读时会从内存更新自己的cache行. 至于
: 多个核同时修改同一段内存对应的cache,哪一个核会先更新该内存,cpu有自己的策略,
: 由于同时性这个不能用一致性来评价, 而且更重要的这个应该由应用程序来确保,cpu没
: 法也没必要做保证,因此并行模型的同步机制才显得很重要.
:
: 已经

n******t
发帖数: 4406
124
找个程序看看不就知道了。。。
这种问题,就算你知道原理也没用。。

全局

【在 r****o 的大作中提到】
: 请问,在multithreading环境下,假如用了mutex来保护某个全局变量,是不是对该全局
: 变量仍然要加volatile关键字才能保证该变量的值是没有被其他线程改过的?

1 (共1页)
进入Programming版参与讨论
相关主题
多线程编程前景如何?有关objec access path的问题
多线程 编程,process 和 thread 的一些问题。问个C++编译器如何处理函数内的static 变量
怎样提高C#计算程序的performance?C 多线程的一个问题
推荐一下C++多线程的书吧请教一个c语言实现多线程的问题
看了这篇文章,脑子有点不够用了关于多线程编程的一个问题
多线程的程序设计有什么好书推荐? (转载)关于线程读写全局变量的问题
关于多线程锁:锁代码还是锁资源?java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
c++下如何实现多线程?c++posix多线程问题请教
相关话题的讨论汇总
话题: global话题: var话题: volatile话题: mutex话题: c++