Namespaces
Variants

std::jthread:: joinable

From 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
bool joinable ( ) const noexcept ;
(desde C++20)

Comprueba si el objeto std::jthread identifica un hilo de ejecución activo. Específicamente, devuelve true si get_id ( ) ! = std :: jthread :: id ( ) . Por lo tanto, un jthread construido por defecto no es joinable.

Un hilo que ha terminado de ejecutar código, pero aún no ha sido unido todavía se considera un hilo activo de ejecución y por lo tanto es unible.

Contenidos

Parámetros

(ninguno)

Valor de retorno

true si el objeto std::jthread identifica un hilo de ejecución activo, false en caso contrario.

Ejemplo

#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
void foo()
{
    std::this_thread::sleep_for(500ms);
}
int main()
{
    std::cout << std::boolalpha;
    std::jthread t;
    std::cout << "before starting, joinable: " << t.joinable() << '\n';
    t = std::jthread{foo};
    std::cout << "after starting, joinable: " << t.joinable() << '\n';
    t.join();
    std::cout << "after joining, joinable: " << t.joinable() << '\n';
    t = std::jthread{foo};
    t.detach();
    std::cout << "after detaching, joinable: " << t.joinable() << '\n';
}

Salida:

before starting, joinable: false
after starting, joinable: true
after joining, joinable: false
after detaching, joinable: false

Referencias

  • Estándar C++23 (ISO/IEC 14882:2024):
  • 33.4.4.3 Miembros [thread.jthread.mem]
  • Estándar C++20 (ISO/IEC 14882:2020):
  • 32.4.3.2 Miembros [thread.jthread.mem]

Véase también

devuelve el id del hilo
(función miembro pública)
espera a que el hilo finalice su ejecución
(función miembro pública)
permite que el hilo se ejecute independientemente del manejador del hilo
(función miembro pública)