【发布时间】:2011-01-18 15:13:34
【问题描述】:
可以在所谓的线程感知模式下编译 BOOST 库。如果是这样,您将看到“...-mt...”出现在库名称中。我不明白它给了我什么,我什么时候需要使用这种模式?它能给我带来什么好处吗?
不仅如此,我对在无线程感知机制下编译 BOOST 线程库(名称中没有 -mt)感到非常困惑。这对我来说没有任何意义。看起来自相矛盾:/
非常感谢您的帮助!
【问题讨论】:
标签: c++ multithreading boost
可以在所谓的线程感知模式下编译 BOOST 库。如果是这样,您将看到“...-mt...”出现在库名称中。我不明白它给了我什么,我什么时候需要使用这种模式?它能给我带来什么好处吗?
不仅如此,我对在无线程感知机制下编译 BOOST 线程库(名称中没有 -mt)感到非常困惑。这对我来说没有任何意义。看起来自相矛盾:/
非常感谢您的帮助!
【问题讨论】:
标签: c++ multithreading boost
因为你没有具体说明你是如何构建的,以及在什么平台上构建,所以我将解释整个故事。在 Linux 和 Windows 上,Boost.Thread 库都是以 MT 模式构建的。在 Windows 上,默认情况下,您会获得 -mt 后缀。在 Linux 上,默认情况下在 1.42 中,您没有后缀。在 Linux 上没有后缀的原因是几乎没有其他库使用这种约定,而且无论如何它在 Linux 上的重要性要低得多。
这是否说明了问题?
【讨论】:
可以选择将“-mt”后缀放回去 (bjam --layout=tagged)
--layout=<layout> Determines whether to choose library names
and header locations such that multiple
versions of Boost or multiple compilers can
be used on the same system.
versioned - Names of boost binaries
include the Boost version number, name and
version of the compiler and encoded build
properties. Boost headers are installed in a
subdirectory of <HDRDIR> whose name contains
the Boost version number.
tagged -- Names of boost binaries include the
encoded build properties such as variant and
threading, but do not including compiler name
and version, or Boost version. This option is
useful if you build several variants of Boost,
using the same compiler.
system - Binaries names do not include the
Boost version number or the name and version
number of the compiler. Boost headers are
installed directly into <HDRDIR>. This option
is intended for system integrators who are
building distribution packages.
The default value is 'versioned' on Windows, and
'system' on Unix.
【讨论】:
MT 在 boost 库中启用了多线程支持,这意味着您可以安全地在多线程程序中使用它们(至少从库的内部代码的角度来看)。
确实在“无线程”模式下构建线程库没有任何意义,但我的印象是该特定构建目标已禁用。
看看这些
http://sodium.resophonic.com/boost-cmake/current-docs/build_variants.html
http://www.boost.org/doc/libs/1_41_0/more/getting_started/windows.html#library-naming
【讨论】:
您可以在构建 Boost 时支持或不支持多线程(threading=multi|single)。 Boost.Thread 通过在其 Jamfile(Makefile 的 bjam 等效项)中设置 threading=multi 来强制构建库。
所以不管你是否请求线程支持,Boost.Thread 总是提供它。因此,您可以找到这两个名称。
【讨论】:
由于在 Linux 下,-mt 版本是别名/绑定到常规版本,所以没有区别。在一个普通的现代系统中,为了便于编译,两者都被简单地包含在内。
【讨论】:
我不是 Boost 专家,但我认为是这样的:
在 MT 环境中,任何全局或共享数据都可能有多个线程同时尝试访问它,这可能导致数据损坏。支持 MT 的对象将使用同步(Critical Sections、Mutexes 等)来确保一次只有一个线程可以访问数据。
Boost 线程库中的某些函数可能仍然在单线程程序中工作。或者,这些函数可以解析为 no-ops(无害的无操作函数),以便可以使用 MT(并且 boost 函数工作)或单线程(并且 boost 函数什么都不做)编译相同的程序,而无需更改代码。
【讨论】: