C++ 多线程 std::thread::joinable
C++ 多线程 std::thread::joinable
- 1. `std::thread::joinable`
- 2. Parameters
- 3. Return value
- 4. Examples
- 5. Data races (数据竞争)
- 6. Exception safety (异常安全性)
- References
https://cplusplus.com/reference/thread/thread/joinable/
public member function
1. std::thread::joinable
bool joinable() const noexcept;
Returns whether the thread (std::thread
) object is joinable.
A thread (std::thread
) object is joinable if it represents a thread of execution.
A thread (std::thread
) object is not joinable in any of these cases:
- if it was default-constructed (https://cplusplus.com/reference/thread/thread/thread/).
- if it has been moved from (either constructing another thread object, or
std::thread::operator=
). - if either of its members join (
std::thread::join
) or detach (std::thread::detach
) has been called.
2. Parameters
none
3. Return value
true
if the thread is joinable.
false
otherwise.
4. Examples
#include <iostream>
#include <thread>void forever() {std::cout << "void forever()" << std::endl;
}int main() {std::thread first;std::thread second(forever);std::cout << "Joinable after construction:\n" << std::boolalpha;std::cout << "first: " << first.joinable() << '\n';std::cout << "second: " << second.joinable() << '\n';if (first.joinable()) first.join();if (second.joinable()) second.join();std::cout << "Joinable after joining:\n" << std::boolalpha;std::cout << "first: " << first.joinable() << '\n';std::cout << "second: " << second.joinable() << '\n';return 0;
}
void forever()Joinable after construction:
first: false
second: trueJoinable after joining:
first: false
second: false
请按任意键继续. . .
5. Data races (数据竞争)
The object is accessed.
6. Exception safety (异常安全性)
No-throw guarantee: never throws exceptions.
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] std::thread::joinable
, https://cplusplus.com/reference/thread/thread/joinable/