在多线程感知模式下使用BOOST库

29

BOOST库有可能以所谓的“线程感知模式”编译。如果是这样,你会在库名称中看到“...-mt...”。我不明白它对我有什么用处,以及何时需要使用这种模式?它是否会给我带来任何好处?

更重要的是,我非常困惑 BOOST Threads 库是在没有线程感知模式下编译的(名称中没有 -mt)。对我来说这没有任何意义。看起来自相矛盾 :/

非常感谢任何帮助!

6个回答

20
因为您没有说明如何构建以及在哪个平台上进行构建,所以我会解释整个过程。Linux和Windows都是使用MT模式构建Boost.Thread库。在Windows上,默认情况下,你会得到-mt后缀。在Linux上,默认情况下,在1.42版本中,你不会得到后缀。在Linux上不使用后缀的原因是几乎没有其他库使用这种约定,而且在Linux上这个约定也不太重要。
这样清楚了吗?

根据你所说的,1.42版本中的命名规范已经改变。实际上,这与我所看到的一致。从现在开始,它会成为一个标准规范吗?(我的意思是在Linux上名称中不再有“-mt”)谢谢Vladimir! - musthero
是的,从现在开始,在Linux上使用默认选项构建将生成没有-mt的库。 - Vladimir Prus
1
那么,你如何区分支持线程的库和普通库呢? - Jeroen
你无法知道其他系统库通常是使用哪些标志进行构建的,就像你无法知道这一点一样。 - Vladimir Prus
1
我每天都更加讨厌C和C++的生态系统。 - rr-

18

有一个选项可以将"-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.

3

谢谢,Mario。这很有帮助!然而,我仍然不知道为什么我能够编译线程库而没有'-mt',如果默认启用它只能在此模式下进行编译(这是我在lib文件夹中拥有的内容- libboost_thread.so.1.42.0)。我只是按照BOOST网页上的说明进行操作。 - musthero

3

你可以选择是否构建带有多线程支持的 Boost(threading=multi|single)。Boost.Thread 通过在其 Jamfile 中设置 threading=multi 强制构建该库(相当于 Makefile 中的 bjam)。

因此,无论您是否请求线程支持,Boost.Thread 都会提供它。因此,您可以找到两个名称。


1

在Linux下,-mt版本与常规版本是别名/绑定的,没有区别。在一个原始的现代系统中,两者仅仅是为了编译方便而包含。


0

我不是Boost大师,但我认为它是这样的:

在多线程环境中,任何全局或共享数据可能会有多个线程同时尝试访问它,这可能导致数据损坏。MT-aware对象将使用同步(关键节,互斥等)来确保一次只有一个线程可以访问数据。

Boost线程库中可能仍然有适用于单线程程序的函数。或者,这些函数可能解析为无操作(无害的无操作函数),以便相同的程序可以编译为MT(并且boost函数有效)或单线程(并且boost函数不起作用)而无需更改代码。


1
是的,这很有道理。然而我仍然对一个没有“-mt”命名的线程库感到困惑。他们说:“boost_thread项目的SINGLE_THREADED变体已禁用。”但我在我的文件夹中没有“- libboost_thread.so.1.42.0 -”。这是一个错误还是我做错了什么? - musthero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接