operator==,!=,<,<=,>,>=,<=>(std::thread::id)

来自cppreference.com
< cpp‎ | thread‎ | thread‎ | id


 
 
并发支持库
线程
(C++11)
(C++20)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
互斥
(C++11)
(C++11)  
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩与屏障
(C++20)
(C++20)
未来体
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
(C++26)
风险指针





原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(C++20 中弃用)
(C++11)(C++20 中弃用)
内存定序
原子操作的自由函数
原子标志的自由函数
 
 
std::thread::id
成员函数
非成员函数
operator==operator!=operator<operator<=operator>operator>=operator<=>
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
辅助类
 
在标头 <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) 检查 lhsrhs 是否表示同一线程或均不表示线程。
3-7) 在未指明的全序中比较 lhsrhs

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(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

参阅