std::jthread:: joinable
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
|
jthread::joinable
|
||||
| Operations | ||||
| Stop token handling | ||||
| Non-member functions | ||||
|
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) |