什么是并发的C ++ 03内存模型? [英] What is the C++03 memory model for concurrency?

查看:139
本文介绍了什么是并发的C ++ 03内存模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03中的并发内存模型是什么?



(而且,C ++ 11是否更改了内存模型以更好地支持并发?)

解决方案

C ++内存模型是物理内存相对于C ++代码读/写的时间和原因的规范。 >

直到下一个C ++标准,C ++内存模型与C相同。在C ++ 0x标准中,期望包括用于多线程的适当的内存模型a href =http://en.wikipedia.org/wiki/C%2B%2B0x#Multithreading_memory_model =nofollow>此处),并且它将成为C的下一个版本的一部分标准,C1X。当前的一个是初步的:




  • 它只指定当前程序可观察的内存操作的行为。

  • 当多个进程访问同一内存时,并没有说明并发内存访问(没有共享内存或进程的概念)。

  • 它没有说什么

  • 它没有提供指定内存访问顺序的方法(编译器优化包括代码运动和最近的处理器)重排序访问,都可以破坏模式,例如双重检查初始化)。


所以,当前的状态是:C ++内存操作只有当你有一个进程,它的主线程和不写代码,对可变读/写的特定排序就是这样。



当然,你会提示添加它的工作原理今天在我的机器上,你不可能是对的。正确的句子将是它今天在我的机器上这种特定的硬件,操作系统(线程库)和编译器的组合,谁知道彼此足够实现的东西,有点工作,但可能会在某一点破裂



好吧,这有点严苛,但他还是个地狱,甚至Herb Sutter承认(只是阅读介绍),他说的是2007年之前的版本的最无处不在的C / C ++工具链...



C ++标准委员会试图提出一些解决所有问题的方法,



Hans Boehm收集了这里一些关于这个问题的论文,包括学术界和C ++委员会的论文。


What is the memory model for concurrency in C++03?

(And, does C++11 change the memory model to support concurrency better?)

解决方案

The C++ memory model is the specification of when and why physical memory is read/written with respect to C++ code.

Until the next C++ standard, the C++ memory model is the same as C. In the C++0x standard, a proper memory model for multithreading is expected to be included (see here), and it will be part possibly of the next revision of the C standard, C1X. The current one is rudimentary:

  • it only specifies the behavior of memory operations observable by the current program.
  • it doesn't say anything about concurrent memory accesses when multiple processes access the same memory (there is no notion of shared memory or processes).
  • it doesn't say anything about concurrent memory accesses when multiple threads access the same memory (there is no notion of threads).
  • it offers no way to specify an ordering for memory accesses (compiler optimizations include code motion and recent processors reorder accesses, both can break patterns such as double checked initialization).

So, the current state is: C++ memory operations are only specified when you have 1 process, with its main thread and don't write code which depends on a specific ordering of variable read/writes and that's it. In essence, this means that aside from the traditional hello world program you're screwed.

Of course, you'll be prompt to add that "it works today on my machine, you can't possibly be right". The correct sentence would be "it works today on my machine with this specific combination of hardware, operating system (thread library) and compiler who know enough of each other to implement something which is somewhat working but will probably break at some point".

Ok ok, this is a bit harsh but hell, even Herb Sutter acknowledges that (just read the intro) and he is talking about all pre 2007 versions of one of the most ubiquitous C/C++ toolchain...

The C++ standard committee attempts to come up with something which will address all those issues while still being less constraining (and thus better performing) than Java's memory model.

Hans Boehm has collected here some pointers to papers on the issue, both academic, and from the C++ committee.

这篇关于什么是并发的C ++ 03内存模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆