core dump when running simple std::thread code using gcc 4.6 and 4.7 on linux

c++, multithreading

Solution

You have to compile using the `-pthread` option.

g++ -std=c++11 -pthread -o main main.cpp

Problem

I don't know what's wrong my simple std::thread code (listed below). It always crashes when using gcc 4.6 or the latest 4.7 on Ubuntu. I compiled it with command `g++ -std=c++11 myfile.cpp` and `g++ -std=gnu++11 myfile.cpp`. ``` #include <iostream> #include <thread> using namespace std; void func() { cout << "hello\n"; } int main() { std::thread thrd(func); thrd.join(); } ``` The callstack of the core dump is something like below ``` #0 0x00007ffff7539445 in raise () from /lib/x86_64-linux-gnu/libc.so.6 #1 0x00007ffff753cbab in abort () from /lib/x86_64-linux-gnu/libc.so.6 #2 0x00007ffff7b35b0d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff7b33c16 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff7b33c43 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff7b33e6e in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff7b8829c in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #7 0x00007ffff7b89132 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #8 0x000000000040118e in std::thread::thread<void (&)()> (this=0x7fffffffdeb0, __f= @0x400e2c: {void (void)} 0x400e2c <func()>) at /usr/include/c++/4.7/thread:133 #9 0x0000000000400e5b in main () at main2.cpp:13 ```

Original source

Related problems