id_operator_cmp

id_operator_cmp (스레드 ID 비교 연산자)

이 페이지는 std::thread::id 객체들을 비교하는 연산자들에 대해 설명해요. ==, !=, <, <=, >, >=, <=> 연산자를 통해 스레드 식별자를 서로 비교할 수 있어요. C++11부터 제공되며, C++20에서 우주선 연산자 <=>가 추가되었어요.

출처: cppreference

본문

개요

<thread> 헤더에 정의된 두 스레드 식별자를 비교하는 연산자들이에요.

<thread> 헤더에 정의됨
bool operator == ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (1) (since C++11)
bool operator != ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (2) (since C++11) (until C++20)
bool operator < ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (3) (since C++11) (until C++20)
bool operator <= ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (4) (since C++11) (until C++20)
bool operator > ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (5) (since C++11) (until C++20)
bool operator >= ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (6) (since C++11) (until C++20)
std :: strong_ordering operator <=> ( std :: thread :: id lhs , std :: thread :: id rhs ) noexcept ; (7) (since C++20)

두 스레드 식별자를 비교해요.

<, <=, >, >=, != 연산자는 각각 operator<=>operator==로부터 합성돼요. (since C++20)

매개변수

lhs, rhs - 비교할 스레드 식별자

반환값

비교 결과에 따라 bool 값 또는 std::strong_ordering 값을 반환해요.

복잡도

상수 시간이에요.

예제

#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

같이 보기

C 문서의 thrd_equal

더 알아보기 (Learn more)

cppreference