What Are the Correct Link Options to Use Std::Thread in Gcc Under Linux

What are the correct link options to use std::thread in GCC under linux?

I think on Linux pthread is used to implement std::thread so you need to specify the -pthread compiler option.

As this is a linking option, this compiler option need to be AFTER the source files:

$ g++ -std=c++0x test.cpp -pthread

Compiling multithread code with g++

The answer was provided by a kind member of SO C++ chat.

It looks like this behaviour is caused by a bug in gcc.

The workaround provided in the last comment of that bug discussion does work and solves the issue:

-Wl,--no-as-needed

Which C++ standard library headers invoke a requirement for the -pthread option with GCC?

<thread> <mutex> <condition_variable> <future> <shared_mutex> <stop_token> all use Pthreads types and functions.

The Networking TS headers such as <experimental/net> and <experimental/executor> use <mutex> and <future> so they also depend on Pthreads.

We'll soon be adding more concurrency headers such as <latch> <barrier> <semaphore>.

Basically anything related to concurrency and synchronization, except <atomic> which notably does not depend on anything from Pthreads.

On the website: godbolt.org, and only there: how can I use std::thread?

In all likelyhood, thread creation is disabled on godbolt.org on purpose (to prevent denial of service attacks or other abuse), so there is currently no way to use std::thread on that service.



Related Topics



Leave a reply



Submit