在Linux上推动的正确链接多线程库 [英] proper linking of boost multithreaded libraries on Linux

查看:451
本文介绍了在Linux上推动的正确链接多线程库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑是否要对libboot _ *链接 - MT变体或不和他们实际上是用于

我使用升压1.46.0的自定义补丁包,在CentOS 6构建产生/usr/lib64/libboost_thread-mt.so.7以及-mt和标准版本的其他库。

我写它使用一个线程来存储一个boost ::未来的计算单元测试程序。要链接的测试,我不得不添加-lboost_thread-MT。但我没有需要改变其他升压-l args设置为使用-mt版本。

我读过href=\"http://www.boost.org/doc/libs/1_41_0/more/getting_started/windows.html#library-naming\" rel=\"nofollow\">库命名的
部分在提升网站,但它并不清楚我是什么表示该库是多线程启用内建支持。不支持多线程可以通过没有-mt确定建库的实际含义。


  1. 我是否需要,如果我有-lboost_thread-MT链接切换到其他库的多线程感知的版本?如果没有,当我需要反对-mt变种链接?


  2. 有没有反对-mt选择一个连接变种的建议只有在需要他们?本项目采用GNU作出的构建。


  3. 有没有一直反对-mt变种连接性能或功能处罚?



解决方案

当你的bjam线设置线程=多的MT变种建成。其中的MT选项的后果是, BOOST_HAS​​_THREADS 定义。

当然,你链接shound是相同的变种,您的应用程序的线程相匹配的升压库。否则,你可以使用ODR违反结束:想象一下,一个编译的shared_ptr 在库中没有 BOOST_HAS​​_THREADS ,而图书馆乙 - 与 BOOST_HAS​​_THREADS 。这两个的shared_ptr 的有完全不同的实现自旋锁类。所以,如果你从lib目录获得的shared_ptr ,并把它传递给LIB B,你的程序崩溃。此外,MT和非MT变种可能会使用不同的堆。

(这是说,这是值得一提的BOOST_HAS​​_THREADS取决于一些其他的宏,甚至可以在非MT的变体来定义,所以混合与非MT可以偶尔工作吨 - 但不要依赖于此。 )

至于性能损失 - 很明显,MT变体可以是有点慢

更新:我就假设 BOOST_HAS​​_THREADS 似乎是错误的。不过,这是一个坏主意,混合MT /非吨的变种,如线程=多影响升压ABI。

I'm a bit confused about whether to link against the libboot_*-mt variants or not and what they're actually used for.

I'm using a custom backport of boost 1.46.0 on Centos 6. The build produces /usr/lib64/libboost_thread-mt.so.7 as well as -mt and standard versions of the other libraries.

I've written a unit test program which uses a thread to store a calculation in a boost::future. To link that test I had to add -lboost_thread-mt. But I did not need to change the other boost -l args to use the -mt versions.

I've read the Library Naming section on the boost site but it's not clear to me what "indicates that the library was built with multithreading support enabled. Libraries built without multithreading support can be identified by the absence of -mt" actually means.

  1. Do i need to switch to the multithreading-aware versions of the other libraries if I link with -lboost_thread-mt? If not, when do I need to link against the -mt variants?

  2. Is there a recommendation for selectively linking against the -mt variants only if they're needed? This project uses GNU Make for builds.

  3. Is there a performance or functional penalty for always linking against the -mt variants?

解决方案

The "mt" variant is built when you set threading=multi in bjam line. One of the consequences of "mt" option is that BOOST_HAS_THREADS is defined.

Of course, all the boost libs you link shound be of the same variant, which matches the threading of your application. Otherwise you can end up with ODR violation: imagine that one compiles shared_ptr in library A without BOOST_HAS_THREADS, while library B - with BOOST_HAS_THREADS. The two shared_ptr's have completely different implementation of spinlock class. So, if you get shared_ptr from lib A and pass it to lib B, your program crashes. Besides, mt and non-mt variants might use different heaps.

(That's said, it's worth mentioning that BOOST_HAS_THREADS depends on some other macros, and can be defined even in a non-mt variant, so mixing mt with non-mt can occasionally work -- but don't rely on this.)

As for performance penalty - obviously, the mt variant may be a bit slower.

UPDATE: My assumption regarding BOOST_HAS_THREADS appears to be wrong. Still, it's a bad idea to mix mt/non-mt variants, as threading=multi affects Boost ABI.

这篇关于在Linux上推动的正确链接多线程库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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