【发布时间】:2011-09-09 09:24:26
【问题描述】:
是 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();
}
当然,区别在于:
- 不工作的代码使用了 Functor(带有
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 };给函子
【问题讨论】:
-
你在什么系统上运行这个?
-
linux 64bit (ubuntu 10.4 LTS), gcc-4.7.0, svn checkout 从上周开始。
标签: multithreading c++11 gcc4