14

I am just trying my hands on g++ 4.6 and C++11 features. Every time I compile a simple threading code using -std=c++0x flag, either it crashes with segmentation fault or it just throws some weird exception.

I read some questions related to C++11 threads and I realized that, I also need to use -pthread flag to compile the code properly. Using -pthread worked fine and I was able to run the threaded code.

My question is, whether the C++11 multi-threading model uses Pthreads in the background? Or is it written from the scratch?

I don't know if any of the members are gcc contributors but I am just curious.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Recker
  • 1,915
  • 25
  • 55
  • @Kay it looks like the question is about g++ 4.6. – juanchopanza Aug 02 '12 at 19:17
  • 2
    The "some weird exception" is probably `std::system_error`, required by the standard when threads cannot be created (e.g. because you haven't linked to libpthread.so with `-pthread`). I am working on improving the message that exception gives, see http://gcc.gnu.org/PR52681 – Jonathan Wakely Aug 02 '12 at 23:32
  • 1
    @JonathanWakely....Yes, this is the exception that I am dealing with on my Linux machine...with AMD Turion X2....While it went smoothly without `-pthread` flag on my roommates Intel machine... – Recker Aug 02 '12 at 23:56

3 Answers3

23

If you run g++ -v it will give you a bunch of information about how it was configured. One of those things will generally be a line that looks like

Thread model: posix

which means that it was configured to use pthreads for its threading library (std::thread in libstdc++), and which means you also need to use any flags that might be required for pthreads on your system (-pthread on Linux).

This has nothing specific to do with the standard, its just a detail of how the standard is implemented by g++

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 4
    N.B. one of those things will _always_ be that line, if thread support is absent it will say "Thread model: single" – Jonathan Wakely Aug 02 '12 at 23:28
  • Note that the GCC reported thread model also reflects on other parts of GCC, like libgcc, and the compiler itself. Many things may be built on top of that threads implementation, not all equally obvious. GCC is quite "monolithic" in that regard... – rubenvb Jul 17 '14 at 11:26
9

C++ doesn't specify how threads are implemented. In practice C++ threads are generally implemented as thin wrappers over pre-existing system thread libraries (like pthreads or windows threads). There is even a provision to access the underlying thread object with std::thread::native_handle().

  • 2
    G++ is not a unix compiler, it works on Windows too, where it can be configured to use either pthread-win32 or native Windows threads. So far noone has done the work for GCC to make its C++11 thread library work with Windows threads, I posted some ideas to http://gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html – Jonathan Wakely Aug 02 '12 at 23:30
2

The reason that it crashes is that if you do not specify -pthreads or -lpthreads, a number of weakly defined pthreads stub functions from libc are linked. These stub functions are enough to get your program to link without error. However, actually creating a pthread requires the full on libpthread.a library, and when the dynamic linker (dl) tries to resolve those missing functions, you get a segmentation violation.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123