boost::thread:运行优化版本时出现段错误
我无法让 boost:thread
正常工作。 在没有优化的情况下编译时它运行良好:
g++ -o test-thread test-thread.cpp -lboost_thread-gcc-mt-s -lpthread
./test-thread
但是使用优化编译的版本会崩溃
g++ -O2 -o test-thread test-thread.cpp -lboost_thread-gcc-mt-s -lpthread
./test-thread
Segmentation fault
有人知道可能是什么原因吗?
这是我正在使用的代码:
#include <boost/thread.hpp>
#include <boost/function.hpp>
void task1() {
// do something
}
void task2() {
// do something
}
int main (int argc, char ** argv) {
using namespace boost;
function0<void> f1(&task1);
function0<void> f2(&task2);
thread thread_1(f1);
thread thread_2(f2);
// do other stuff
thread_2.join();
thread_1.join();
return 0;
}
PS:我在 ubuntu linux 上使用 boost 1.32。
更新:
这是它在调试器中崩溃的地方(其中第 37 行是我的原始代码中带有 thread_2.join();
的行):
(gdb) bt
#0 0x080499e0 in boost::thread::join ()
#1 0x080496b8 in main (argc=1, argv=0xbfea3eb4) at ../src/test-thread.cpp:37
这是我实际的两个函数:
void task1() {
std::cerr << "THREAD 1 START" << std::endl;
for(double i=0; i<999999; ++i){
std::cout << i << std::endl;
}
std::cerr << "THREAD 1 END" << std::endl;
}
void task2() {
std::cerr << "THREAD 2 START" << std::endl;
for(double i=0; i<999999; ++i){
std::cout << i << std::endl;
}
std::cerr << "THREAD 2 END" << std::endl;
}
谢谢你的帮助!
I have trouble getting boost:thread
to work. It runs fine when compiling without optimization:
g++ -o test-thread test-thread.cpp -lboost_thread-gcc-mt-s -lpthread
./test-thread
But a version that's compiled with optimizations crashes
g++ -O2 -o test-thread test-thread.cpp -lboost_thread-gcc-mt-s -lpthread
./test-thread
Segmentation fault
Does anyone know what might be the reason?
Here is the code I am using:
#include <boost/thread.hpp>
#include <boost/function.hpp>
void task1() {
// do something
}
void task2() {
// do something
}
int main (int argc, char ** argv) {
using namespace boost;
function0<void> f1(&task1);
function0<void> f2(&task2);
thread thread_1(f1);
thread thread_2(f2);
// do other stuff
thread_2.join();
thread_1.join();
return 0;
}
P.S.: I am using boost 1.32 on ubuntu linux.
UPDATE:
Here is where it crashes in the debugger (where line 37 is the one with thread_2.join();
in my original code):
(gdb) bt
#0 0x080499e0 in boost::thread::join ()
#1 0x080496b8 in main (argc=1, argv=0xbfea3eb4) at ../src/test-thread.cpp:37
Here are my actual two functions:
void task1() {
std::cerr << "THREAD 1 START" << std::endl;
for(double i=0; i<999999; ++i){
std::cout << i << std::endl;
}
std::cerr << "THREAD 1 END" << std::endl;
}
void task2() {
std::cerr << "THREAD 2 START" << std::endl;
for(double i=0; i<999999; ++i){
std::cout << i << std::endl;
}
std::cerr << "THREAD 2 END" << std::endl;
}
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了这个错误! 我链接了错误版本的库。 我一直在使用
boost_thread-gcc-mt-s
,但它可以与boost_thread-gcc-mt
配合使用:我查看了 boost 文档,但找不到有关这些库版本之间差异的任何信息。 还有一个
boost_thread-gcc-mt-d
,我认为它是调试版本,但链接到该版本总是会导致出现段错误的二进制文件,即使使用-g
进行编译也是如此代码>. 但至少我现在可以运行线程了。I found the bug! I had linked against the wrong version of the library. I had been using
boost_thread-gcc-mt-s
, but it works withboost_thread-gcc-mt
instead:I looked into the boost documentation, but I couldn't find any information about what the differences between those library versions are. There is also a
boost_thread-gcc-mt-d
, which I assume is the debug version, but linking against that one always results in binaries that segfault, even when compiling with-g
. But at least I can run the threads now.您能否确保核心转储处于活动状态(
ulimit -c unlimited
)、使用符号编译(-O2 -g)、运行并在 gdb 中打开堆栈跟踪? (gdb test-thread core
,然后在gdb
提示符下键入backtrace
,quit
退出)此类问题的常见原因:
易失性 的两个线程都访问了未初始化的变量内
Could you make sure that core dumps are active (
ulimit -c unlimited
), compile with symbols (-O2 -g), run, and open the stack trace in gdb? (gdb test-thread core
, then typebacktrace
at thegdb
prompt,quit
to quit)Usual causes for these kinds of problems:
volatile