8

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?

1
  • 1
    How std::thread is implemented is not something you should care about. Just use it. Commented Nov 10 at 14:57

4 Answers 4

5

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

  1. They call a platform-specific thread creation function.

On Linux, it is pthread_create from POSIX Threads API. You can call this function directly. See man pthreads for more details.

On Windows, that's _beginthread and _beginthreadex, which you can can call directly too.

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 neither necessary nor sufficient in both C and C++).

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

std::thread_local is a portable C++ storage class specifier which is an alias to a platform-specific thread-local storage class specifier. Before its introduction in C++11 people used platform-specific keywords directly for thread-local storage. Portability of thread-local storage to other platforms required writing/using/maintaining macros which expanded to platform-specific keywords for thread local storage class specifier.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

2 Comments

The OP asked about a specific implementation (how gcc/clang implements std::thread), so quoting the standard is not a relevant response.
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.
1

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

2 Comments

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.
Also look at these comments on Reddit, I also found them helpful and similar C1 C2. and one from quora

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.