Search code examples
c++clangllvmc++20clang++

Why is jthread not presented in libc++ (using clang 18, c++26)


the following code just compile with "jthread not in std". with clang 18

clang++ -std=c++26 -stdlib=libc++

and the implementation of jthread is in __thread/jthread.h, but for some reason macro out. why?

#include <thread>
using namespace std::chrono_literals;

int main() {
  std::jthread t([] { std::this_thread::sleep_for(1s); });
}

Solution

  • The libc++ documentation has a webpage about C++20 status of libc++. Quoting the relevant footnote 8:

    P0660: The paper is implemented but the features are experimental and can be enabled via -fexperimental-library.

    Thus, just pass that flag mentioned above when compiling with Clang 18 or newer, like this:

    clang++ -std=c++26 -stdlib=libc++ -fexperimental-library