std::jthread::native_handle
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Concurrency support library
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
(C++26)
(C++20)
(C++26)
(C++20)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++11)
(C++11)
(C++17)
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)(C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++26)
(C++26)
(C++26)
(C++11)
(C++20)
(C++11)
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
(C++11)
(C++11)
(C++11)(deprecated in C++26)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++26)(C++26)
(C++26)(C++26)
(C++11)
(C++20)(C++20)
(C++20)
(C++20)
(C++11)(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
std::jthread
Member functions
Observers
jthread::native_handle
Operations
Stop token handling
Non-member functions
native_handle_type native_handle();
(since C++20) (not always present)
Returns the implementation defined underlying thread handle.
Contents
[edit] Parameters
(none)
[edit] Return value
Implementation defined handle type representing the thread.
[edit] Exceptions
May throw implementation-defined exceptions.
[edit] Example
Uses native_handle
to enable realtime scheduling of C++ threads on a POSIX system.
Run this code
#include <chrono> #include <cstring> #include <iostream> #include <mutex> #include <pthread.h> #include <thread> std::mutex iomutex; void f(int num) { std::this_thread::sleep_for (std::chrono::seconds (1)); sched_param sch; int policy; pthread_getschedparam(pthread_self(), &policy, &sch); std::lock_guard <std::mutex > lk(iomutex); std::cout << "Thread " << num << " is executing at priority " << sch.sched_priority << '\n'; } int main() { std::jthread t1(f, 1), t2(f, 2); sched_param sch; int policy; pthread_getschedparam(t1.native_handle(), &policy, &sch); sch.sched_priority = 20; if (pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) std::cout << "Failed to setschedparam: " << std::strerror (errno) << '\n'; }
Output:
Thread 2 is executing at priority 0 Thread 1 is executing at priority 20