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!