std::jthread:: joinable
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
|
jthread::joinable
|
||||
| Operations | ||||
| Stop token handling | ||||
| Non-member functions | ||||
|
bool
joinable
(
)
const
noexcept
;
|
(depuis C++20) | |
Vérifie si l'objet
std::jthread
identifie un thread d'exécution actif. Plus précisément, retourne
true
si
get_id
(
)
!
=
std
::
jthread
::
id
(
)
. Ainsi, un
jthread
construit par défaut n'est pas joignable.
Un thread qui a terminé l'exécution du code, mais n'a pas encore été joint est toujours considéré comme un thread d'exécution actif et est donc joignable.
Table des matières |
Paramètres
(aucun)
Valeur de retour
true
si l'objet
std::jthread
identifie un thread d'exécution actif,
false
sinon.
Exemple
#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'; }
Sortie :
before starting, joinable: false after starting, joinable: true after joining, joinable: false after detaching, joinable: false
Références
- Norme C++23 (ISO/IEC 14882:2024) :
-
- 33.4.4.3 Membres [thread.jthread.mem]
- Norme C++20 (ISO/CEI 14882:2020) :
-
- 32.4.3.2 Membres [thread.jthread.mem]
Voir aussi
|
retourne l'
id
du thread
(fonction membre publique) |
|
|
attend la fin de l'exécution du thread
(fonction membre publique) |
|
|
permet au thread de s'exécuter indépendamment du gestionnaire de thread
(fonction membre publique) |