C ++ 0x: thread, gcc or my error?

Is it GCC 4.7.0 , or is it me? What am I doing wrong?

This std::system_error exception to the std::system_error "operation:

 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(); }; 

while it works:

 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(); } 

Of course, the difference:

  • Inoperative code uses Functor (class with operator() )
  • The working code uses a function pointer.

Am I using the functor incorrectly somewhere? I don’t see where, right? This is a hint that gdb has this on its stack:

 #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> 

Notes: I also tried

  • Initialize DumbFib first by giving it the member variable n_=35 , the same result.
  • Providing a functor directly with thread th{ DumbFib, 35 }; or thread th{ DumbFib{}, 35 };
+8
multithreading c ++ 11 gcc4
source share
2 answers

When compiling code with g++ use the -pthread .

+13
source share

I also had a similar problem, and thanks to Jason, he solved my problem

Exact parameters will be

 g++ code.cpp -lpthread -std=c++0x 

What should I do in g ++ version 4.6.3

0
source share

All Articles