[PATCH] libstdc++: Enable <thread> without gthreads

Jonathan Wakely jwakely@redhat.com
Wed Nov 11 17:31:07 GMT 2020


On 11/11/20 16:13 +0000, Jonathan Wakely wrote:
>This makes it possible to use std::thread in single-threaded builds.
>All member functions are available, but attempting to create a new
>thread will throw an exception.
>
>The main benefit for most targets is that other headers such as <future>
>do not need to include the whole of <thread> just to be able to create a
>std::thread. That avoids including <stop_token> and std::jthread where
>not required.

I forgot to mention that this patch also reduces the size of the
<thread> header, by only including <bits/unique_ptr.h> instead of the
whole of <memory>. That could be done separately from the rest of the
changes here.

It would be possible to split std::thread and this_thread::get_id()
into a new header without also making them work without gthreads.

That would still reduce the size of the <future> header, because it
wouldn't need the whole of <thread>. But it wouldn't get rid of
preprocessor checks for _GLIBCXX_HAS_GTHREADS in <stop_token>.

Allowing std::this_thread::get_id() and std::this_thread::yield() to
work without threads seems worth doing (we already make
std::this_thread::sleep_until and std::this_thread::sleep_for work
without threads).



More information about the Gcc-patches mailing list