【发布时间】:2015-04-24 19:01:09
【问题描述】:
我在使用带有 clang 的 boost_threads 时遇到问题。 clang 版本是 3.6.0,boost 版本是新 Ubuntu 15.04 的 1.55.0。曾经与以前版本的 clang 一起工作的程序现在在启动时会出现段错误。改用g++没有问题。
这里有一个示例程序来说明这一点。
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void output() {
try {
int x = 0;
for (;;) {
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
cerr << x++ << endl;
}
} catch (boost::thread_interrupted&) {}
}
int main(int argc, char* argv[]) {
try {
boost::thread output_worker(output);
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
output_worker.interrupt();
output_worker.join();
} catch (...) {
cerr << "Unexpected error!" << endl;
exit(1);
}
}
如果我用 g++ 编译它,它可以工作,即
g++ thread.cpp -lboost_thread -lboost_system
如果我用 clang 编译它
clang++ thread.cpp -lboost_thread -lboost_system
下面的 gdb 跟踪出现段错误
Starting program: /home/dejan/test/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd0580 in boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>() ()
from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
(gdb) bt
#0 0x00007ffff7bd0580 in boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>() ()
from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
#1 0x00007ffff7bcb16a in ?? () from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
#2 0x00007ffff7de95ba in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffdf98, env=env@entry=0x7fffffffdfa8)
at dl-init.c:72
#3 0x00007ffff7de96cb in call_init (env=<optimized out>, argv=<optimized out>, argc=<optimized out>, l=<optimized out>) at dl-init.c:30
#4 _dl_init (main_map=0x7ffff7ffe188, argc=1, argv=0x7fffffffdf98, env=0x7fffffffdfa8) at dl-init.c:120
#5 0x00007ffff7dd9d0a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#6 0x0000000000000001 in ?? ()
#7 0x00007fffffffe2fe in ?? ()
#8 0x0000000000000000 in ?? ()
我是不是做错了什么?
【问题讨论】:
-
clang 3.6 和 boost 1.55 之间可能存在兼容性问题。根据 1.55 的 boost 发行说明,它使用高达 3.3 的 clang 版本进行了测试。我在 OSX 上使用 clang 3.6 和 boost 1.58 尝试了你的代码,它似乎工作正常。
-
为什么不使用 -std=c++11 和 std::thread 呢?在这种情况下,您很可能只需使用 std 重命名 boost
-
@Ferruccio:更可能是
g++和clang++之间的不兼容,因为Ubuntu 中的软件包通常使用gcc/g++编译。 -
尝试添加
-pthread选项。
标签: c++ boost clang boost-thread clang++