【问题标题】:BOOST libraries in multithreading-aware mode多线程感知模式下的 BOOST 库
【发布时间】:2011-01-18 15:13:34
【问题描述】:

可以在所谓的线程感知模式下编译 BOOST 库。如果是这样,您将看到“...-mt...”出现在库名称中。我不明白它给了我什么,我什么时候需要使用这种模式?它能给我带来什么好处吗?

不仅如此,我对在无线程感知机制下编译 BOOST 线程库(名称中没有 -mt)感到非常困惑。这对我来说没有任何意义。看起来自相矛盾:/

非常感谢您的帮助!

【问题讨论】:

    标签: c++ multithreading boost


    【解决方案1】:

    因为你没有具体说明你是如何构建的,以及在什么平台上构建,所以我将解释整个故事。在 Linux 和 Windows 上,Boost.Thread 库都是以 MT 模式构建的。在 Windows 上,默认情况下,您会获得 -mt 后缀。在 Linux 上,默认情况下在 1.42 中,您没有后缀。在 Linux 上没有后缀的原因是几乎没有其他库使用这种约定,而且无论如何它在 Linux 上的重要性要低得多。

    这是否说明了问题?

    【讨论】:

    • 根据您的说法,1.42 中的命名约定已更改。确实,这与我所看到的一致。从现在开始,它会成为一个标准的约定吗? (我的意思是在 Linux 的名称中没有 -mt)谢谢 Vladimir!
    • 是的,从现在开始在 Linux 上使用默认选项构建将生成不带 -mt 的库。
    • 那你如何区分线程感知库和常规库?
    • 你不能。就像您一般不知道其他系统库是用哪些标志构建的一样。
    • 我越来越讨厌 C 和 C++ 生态系统。
    【解决方案2】:

    可以选择将“-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】:

      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

      【讨论】:

      • 谢谢,马里奥。这很有帮助!但是,我仍然不知道如何编译没有'-mt'的线程库,如果它默认启用仅在此模式下编译(这是我的lib文件夹中的 - libboost_thread.so.1.42.0 )。我只是按照 BOOST 网页的说明进行操作。
      【解决方案4】:

      您可以在构建 Boost 时支持或不支持多线程(threading=multi|single)。 Boost.Thread 通过在其 Jamfile(Makefile 的 bjam 等效项)中设置 threading=multi 来强制构建库。

      所以不管你是否请求线程支持,Boost.Thread 总是提供它。因此,您可以找到这两个名称。

      【讨论】:

        【解决方案5】:

        由于在 Linux 下,-mt 版本是别名/绑定到常规版本,所以没有区别。在一个普通的现代系统中,为了便于编译,两者都被简单地包含在内。

        【讨论】:

          【解决方案6】:

          我不是 Boost 专家,但我认为是这样的:

          在 MT 环境中,任何全局或共享数据都可能有多个线程同时尝试访问它,这可能导致数据损坏。支持 MT 的对象将使用同步(Critical Sections、Mutexes 等)来确保一次只有一个线程可以访问数据。

          Boost 线程库中的某些函数可能仍然在单线程程序中工作。或者,这些函数可以解析为 no-ops(无害的无操作函数),以便可以使用 MT(并且 boost 函数工作)或单线程(并且 boost 函数什么都不做)编译相同的程序,而无需更改代码。

          【讨论】:

          • 是的,这是有道理的。但是,我仍然对我的线程库名称中没有 -mt 的事实感到困惑。他们说“boost_thread 项目的 SINGLE_THREADED 变体已禁用。”但我的文件夹中没有 - libboost_thread.so.1.42.0 -。是错误还是我做错了什么?
          猜你喜欢
          • 2012-03-06
          • 1970-01-01
          • 2014-10-21
          • 1970-01-01
          • 2010-10-21
          • 2017-05-14
          • 2017-08-07
          • 1970-01-01
          • 2019-11-13
          相关资源
          最近更新 更多