operator==,!=,<,<=,>,>=,<=>(std::thread::id)
来自cppreference.com
在标头 <thread> 定义
|
||
bool operator==( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(1) | (C++11 起) |
bool operator!=( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(2) | (C++11 起) (C++20 前) |
bool operator< ( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(3) | (C++11 起) (C++20 前) |
bool operator<=( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(4) | (C++11 起) (C++20 前) |
bool operator> ( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(5) | (C++11 起) (C++20 前) |
bool operator>=( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(6) | (C++11 起) (C++20 前) |
std::strong_ordering operator<=>( std::thread::id lhs, std::thread::id rhs ) noexcept; |
(7) | (C++20 起) |
比较两个线程标识符。
1,2) 检查 lhs 与 rhs 是否表示同一线程或均不表示线程。
3-7) 在未指明的全序中比较 lhs 与 rhs。
|
(C++20 起) |
参数
lhs, rhs | - | 要比较的线程标识符 |
返回值
1-6) 若相应的关系成立则为 true,否则为 false。
7) 若在该全序中 lhs 小于 rhs 则为 std::strong_ordering::less;否则若在该全序中 rhs 小于 lhs 则为 std::strong_ordering::greater ;否则为 std::strong_ordering::equal。
复杂度
常数。
示例
运行此代码
#include <cassert> #include <chrono> #include <iostream> #include <thread> int main() { auto work = [] { std::this_thread::sleep_for(std::chrono::seconds(1)); }; std::thread t1(work); std::thread t2(work); assert(t1.get_id() == t1.get_id() and t2.get_id() == t2.get_id() and t1.get_id() != t2.get_id()); if (const auto cmp = t1.get_id() <=> t2.get_id(); cmp < 0) std::cout << "id1 < id2\n"; else std::cout << "id1 > id2\n"; std::cout << "id1: " << t1.get_id() << "\n" "id2: " << t2.get_id() << '\n'; t1.join(); t2.join(); }
可能的输出:
id1 > id2 id1: 139741717640896 id2: 139741709248192