Namespaces
Variants

std::jthread:: native_handle

From es.cppreference.net

Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
native_handle_type native_handle ( ) ;
(desde C++20)
(no siempre presente)

Devuelve el identificador de subproceso subyacente definido por la implementación.

Contenidos

Parámetros

(ninguno)

Valor de retorno

Tipo de manejador definido por la implementación que representa el hilo.

Excepciones

Puede lanzar excepciones definidas por la implementación.

Ejemplo

Utiliza native_handle para habilitar la planificación en tiempo real de hilos C++ en un sistema POSIX.

#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';
}

Salida:

Thread 2 is executing at priority 0
Thread 1 is executing at priority 20