Code:
#include
#include
void hello()
{
std::cout << \"Hello World\" << std::endl;
}
int main()
{
try
Your problem is that you forgot to specify -lpthread
or -pthread
flag to the compiler. So it assumes a single-threaded mode when building your program.
The exception is thrown by the standard C++ library:
(gdb) catch throw
Function "__cxa_throw" not defined.
Catchpoint 1 (throw)
(gdb) run
Starting program: /tmp/test
creating thread
Catchpoint 1 (exception thrown), 0x00007ffff7b8eff0 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) where
#0 0x00007ffff7b8eff0 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00007ffff7b3ba3e in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff7b45cb7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00000000004012d4 in std::thread::thread<void (&)()>(void (&&&)()) ()
#4 0x0000000000400f0e in main ()
(gdb) quit
I don't have any desire to check source code for that, but it is most likely they use "lazy" dynamic linking to determine if POSIX threads family of functions are available. And throw exceptions otherwise. That way, you get that exception unless linking with pthread library.
That has nothing to do with virtual memory or other resource limits as I initially thought might be happening because system calls do not report any errors. So just do:
g++ -std=c++0x -o test ./test.cpp -pthread
... and it will work.
UPDATE:
As @ildjaRN pointed out, you already specify -pthread. I suggest you specify it after your object files (source files for a single invocation compile & link), otherwise it might not get picked up.
Here is how to make sure it is picked up - you can run ldd
and make sure that pthread.so makes it in:
$ g++ -std=c++0x -lpthread -o test ./test.cpp
$ ldd ./test | grep pthread
$ g++ -std=c++0x -o test ./test.cpp -lpthread
$ ldd ./test | grep pthread
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff2a9073000)
$
Hope it helps. Good luck!