It looks like any use of std::thread compiled in a program without -pthread (or
probably some specific macro set by -pthread, but hard to say as -pthread isn't
really documented to begin with) will crash on the spot. For instance, this
code:

#include <thread>

void f() {}

int main()
   {
   std::thread t(f);
   t.join();
   }

will die unless -pthread is set:

$ g++-4.5-20091112 -std=c++0x -ggdb -O0 fail.cpp -o fail

$ gdb ./fail 
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu"...
(gdb) run
Starting program: /home/lloyd/fail 

Program received signal SIGSEGV, Segmentation fault.
0x00007faf94deb459 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) backtrace 
#0  0x00007faf94deb459 in ?? () from /lib64/ld-linux-x86-64.so.2
#1  0x00007faf94df14b5 in ?? () from /lib64/ld-linux-x86-64.so.2
#2  0x00007faf94b87a27 in std::thread::_M_start_thread (this=0x7fff9cff8850,
__b=
          {<std::__shared_ptr<std::thread::_Impl_base,
(__gnu_cxx::_Lock_policy)2u>> = {_M_ptr = 0x1669030, _M_refcount = {_M_pi =
0x1669010}}, <No data fields>})
    at ../../../../gcc-4.5-20091112/libstdc++-v3/src/thread.cc:88
#3  0x0000000000400eed in thread<void (*)()> (this=0x7fff9cff8850, __f=0x400ca4
<f()>)
    at
/usr/local/gcc-4.5-20091112/lib/gcc/x86_64-unknown-linux-gnu/4.5.0/../../../../include/c++/4.5.0/typeinfo:121
#4  0x0000000000400cc4 in main ()
(gdb) 

Some thoughts:

Perhaps -pthread should be implied by -std=c++0x/-std=gnu++0x? (Except at least
nominally one could use std::thread with DCE or Solaris threads, so is -pthread
right anyway?)

At least an assert() failure or (preferably) a compile-time error would be
nice; I can't imagine a much worst compiler diagnostic than having the program
silently crash with a segmentation fault.


-- 
           Summary: [C++0x] Using std::thread without -pthread causes
                    immediate crash
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lloyd at randombit dot net
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42198

Reply via email to