std::jthrea...ative_handle(3)
πΈ NAME πΈ
okay so this page is from the C++ Standard Libary manual, and the main character is std::jthread::native_handle. iykyk π
std::jthread::native_handle-std::jthread::native_handle
β¨ Synopsis β¨
[[nodiscard]] native_handle_type native_handle();
- since C++20 β yes bestie, this one is a C++20 thing.
- not always present β literally not always present, fr fr.
It returns the implementation defined underlying thread handle. that's the whole vibe.
π Parameters π
(none)β literally none, bestie. zero parameters. π«Ά
π Return value π
- Implementation defined handle type representing the thread. that's it. that's the return. iconic.
π Exceptions π
- May throw implementation-defined exceptions. so yeah, implementation-defined exceptions are allowed. ngl that's the entire caveat. π
π¦ Example π¦
This example uses native_handle to enable realtime scheduling of C++ threads on a POSIX system.
// Run this code β and then this code, exactly:
#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
mwah π this page is from http://cppreference.com, version/date 2024.06.10, and the footer says std::jthrea...ative_handle(3). periodt. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms