是GCC 4.7.0 还是我?我做错了什么?
这会引发std::system_error
“操作不允许”例外:
struct DumbFib {
size_t operator()(size_t n) { return fib(n); }
static size_t fib(size_t n) {
return n<2 ? 1 : fib(n-2)+fib(n-1);
}
};
void sample() {
DumbFib dumbfib;
thread th{ dumbfib, 35 }; // <- system_error!
th.join();
};
虽然这有效:
void work(size_t loop) {
for(int l = loop; l>0; --l) {
for(int i = 1000*1000; i>0; --i)
;
cerr << l << "...";
}
cerr << endl;
}
int main() {
//sample();
thread t { work, 100 }; // <- fine
t.join();
}
当然,区别在于:
operator()
的课程) 我在某处错误地使用仿函数吗?我看不到哪里,是吗?是否gdb
在堆栈中有这个提示:
#7 ... in std::thread::_M_start_thread (..., __b=warning: RTTI symbol not found\
for class 'std::_Sp_counted_ptr_inplace<std::thread::_Impl<std::\
_Bind_simple<DumbFib()(int)> >, ..., (__gnu_cxx::_Lock_policy)2>
注意:我也试过
DumbFib
,为其提供成员变量n_=35
,结果相同。thread th{ DumbFib, 35 };
或thread th{ DumbFib{}, 35 };
答案 0 :(得分:13)
使用g++
编译代码时,请使用-pthread
选项。
答案 1 :(得分:0)
我也遇到了类似的问题,感谢Jason,它解决了我的问题
确切的选项将是
g++ code.cpp -lpthread -std=c++0x
我在 g ++版本4.6.3
上要做的事情