【发布时间】:2016-07-29 20:10:54
【问题描述】:
我读过: “线程不能作为库实现”(http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf),我同意那篇文章的主要思想。但是,我有疑问:毕竟,在 C++ 模型(C++11)之前,没有对线程的原生支持。但是,Boost 能够提供一个 Thread 作为库。那么,怎么可能/我误解了什么?
【问题讨论】:
-
C 语言中没有“本机”支持使用 pthread 进行线程化,本文将其用作线程库的示例,该线程库似乎可以工作,但存在以下问题:
The Pthreads specification prohibits races, i.e. accesses to a shared variable while another thread is modifying it. The problem here is that whether or not a race exists depends on the semantics of the programming language, which in turn requires that we have a properly defined memory model. **Thus this definition is circular**.所以线程库存在,并且在实践中有效,但它是 hack-ish。 -
无法将线程高效且正确地实现作为库。 Boost::Thread 遭受了文章中概述的许多问题。同样相关:stackoverflow.com/questions/6319146/…
-
“Pthreads 规范禁止竞争,即在另一个线程修改共享变量时访问它。”。哇,那么它是如何工作的?毕竟,使用互斥锁是一种竞赛,不是吗?
-
这实际上与当前 C++ 规范所说的非原子变量上的数据竞争是 UB 是一样的。两个线程竞相获取互斥锁很好,因为这发生在 pthreads 函数中。这篇论文确实解释了它的术语。
-
@PeterCordes,谢谢 :)
标签: c++ multithreading boost x86