C++11: Is std::thread on linux depending on pthread library?
Asked Answered
C

4

7

I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.

So I wish to know on linux system, how gcc/clang implements std::thread, is it calling some linux native functions/kernel apis or something?

Also, how is std::thread_local implemented, related with __thread?

Condyloma answered 11/8, 2017 at 10:16 Comment(0)
F
1

Use ldd myExecutable on compiler output to find out.

Both libstdc++ and libc++ apparently use pthreads, but they are not required to do that. Evidence of it can be found in native_handle methods documentation here and here. The documents say:

Accesses the native handle of *this.

The meaning and the type of the result of this function is implementation-defined. On a POSIX system, this may be a value of type pthread_cond_t*. On a Windows system, this may be a PCONDITION_VARIABLE.

and

Returns the implementation defined underlying thread handle.

Frumenty answered 11/8, 2017 at 11:45 Comment(2)
The OP asked about a specific implementation (how gcc/clang implements std::thread), so quoting the standard is not a relevant response.Weismann
I quoted the standard to establish, that it does not demand a specific implementation. I also answered that the libs use pthreads and the answer apparently satisfied OP, since it got accepted.Frumenty
H
4

I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.

This information is inaccurate.

how gcc/clang implements std::thread

They call a platform-specific thread creation function. On Linux it is pthread_create. You can call this function directly.

When a thread throws an exception and it is not caught std::terminate is called.

Note that your application must be compiled and linked with -pthread flag (using -lpthread is unnecessary and insufficient with both C and C++).

Highchair answered 11/8, 2017 at 13:49 Comment(0)
B
4

I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.

There's a statement in the neighborhood of this that is true, but this statement as written is not true.

There are two facts here.

  1. If YOU call pthreads functions yourself, it is indeed just a C library, and you had better make sure you do everything correctly in regards to exception safety. If you pass function pointers to pthread_create_... and those functions will throw exceptions... your program can have big problems. That should be obvious, it will be true whenever you talk to a C library from C++.

    That does not mean it is impossible to use such a library with a C++ program!

  2. pthread does not actually need to know about any of your objects, or any of their ctors or dtors, or any of that, in order to make your program multithreaded. All it needs to spawn a thread, is a function pointer, and that function pointer will have a completely C-compatible signature.

    When the C++ compiler calls pthreads functions in order to implement std::thread, the compiler is going to emit code that talks to pthread correctly. If it uses pthread in an illegal way to implement your C++ program, it's a bug in the compiler or standard library.

Basrelief answered 12/8, 2017 at 1:57 Comment(0)
F
1

Use ldd myExecutable on compiler output to find out.

Both libstdc++ and libc++ apparently use pthreads, but they are not required to do that. Evidence of it can be found in native_handle methods documentation here and here. The documents say:

Accesses the native handle of *this.

The meaning and the type of the result of this function is implementation-defined. On a POSIX system, this may be a value of type pthread_cond_t*. On a Windows system, this may be a PCONDITION_VARIABLE.

and

Returns the implementation defined underlying thread handle.

Frumenty answered 11/8, 2017 at 11:45 Comment(2)
The OP asked about a specific implementation (how gcc/clang implements std::thread), so quoting the standard is not a relevant response.Weismann
I quoted the standard to establish, that it does not demand a specific implementation. I also answered that the libs use pthreads and the answer apparently satisfied OP, since it got accepted.Frumenty
J
0

C++ threads are always a wrapper around an underlying library. So if you are using C++ on Linux, the C++ threads /are/ pthreads. Reference

Difference b/w them

Justness answered 15/9 at 11:22 Comment(2)
No. The Linux pthreads lib is just a wrapper around Linux system calls, nothing prevents you per se from implementing a C++ std::thread with no pthread lib in between, though it's not practical.Astrology
Also look at these comments on Reddit, I also found them helpful and similar C1 C2. and one from quoraJustness

© 2022 - 2024 — McMap. All rights reserved.