Apparently if you don't have "-pthread", it would just link in some kind of a stub, that just crashes your program when you call any std::thread function.
Printable View
Apparently if you don't have "-pthread", it would just link in some kind of a stub, that just crashes your program when you call any std::thread function.
t.cpp is the little program above.
Code:$ gcc -ggdb -std=c++0x -lstdc++ -pthread -o t t.cpp
$ ./t
terminate called without an active exception
Aborted
Order matters for linking.
... just tell me what to use, please.
"$ gcc -pthread -std=c++0x -lstdc++ -o t t.cpp" doesn't fix it.
I don't know... I'm on Windows right now.
I only remember I had to play with the order of arguments.
On an unrelated (or maybe related) side note, why are you compiling .cpp with gcc and not g++?
I just SSHed into my box. Without -pthread it segfaults, and with -pthread it aborts. So it's probably something totally unrelated.
Works if I addCode:t.join();
gcc is just a wrapper for all the g* compilers. It calls the appropriate compiler based on file suffix. It's just a habit to use gcc and let it decide which g* compiler should be used.
Come now, cyberfish, I shouldn't be the one to tell you this... but quadruple posting? You know there is an edit button, right?
Tehe :).
I found the problem, it's in the code.
Thread is still running when the destructor gets called, and it doesn't like that.
Code:(gdb) r
Starting program: /tmp/a.out
[Thread debugging using libthread_db enabled]
[New Thread 0xb7d29b70 (LWP 17495)]
terminate called without an active exception
[Thread 0xb7d29b70 (LWP 17495) exited]
Program received signal SIGABRT, Aborted.
0xf57fe416 in __kernel_vsyscall ()
(gdb) bt
#0 0xf57fe416 in __kernel_vsyscall ()
#1 0xb7d55651 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7d58a82 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0xb7fa352f in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib/libstdc++.so.6
#4 0xb7fa1465 in ?? () from /usr/lib/libstdc++.so.6
#5 0xb7fa14a2 in std::terminate() () from /usr/lib/libstdc++.so.6
#6 0x08048a50 in ~thread (this=0xbffff7dc, __in_chrg=<value optimized out>)
at /usr/include/c++/4.4/thread:138
#7 0x080489dd in main () at a.cpp:9
Does the standard say anything about that?
It looks pretty deliberate in the header file -
In /usr/include/c++/4.4/thread line 138.Code:
~thread()
{
if (joinable())
std::terminate();
}
It boggles your mind, but according to...
c++ - thread destructors in C++0x vs boost - Stack Overflow
...it is the correct behavior.
In other words, we're stuck with a useless thread object -_-
Bye, bye RAII...