id_operator_ltlt
id_operator_ltlt (스레드 ID 출력 연산자)
이 페이지는 std::thread::id 객체를 출력 스트림에 쓸 수 있게 해 주는 operator<< 오버로드에 대해 설명해요. 이 연산자를 사용하면 스레드 ID를 로그나 콘솔에 쉽게 출력할 수 있어요. 두 스레드 ID가 같으면 동일한 텍스트 표현이 출력되고, 다르면 서로 다른 표현이 출력돼요.
출처: cppreference
본문
정의 (Definition)
| 항목 | 내용 |
|---|---|
| 헤더 | <thread> |
| 선언 | template < class CharT , class Traits > std :: basic_ostream < CharT , Traits >& operator << ( std :: basic_ostream < CharT , Traits >& ost , std :: thread :: id id ); |
| 도입 | (since C++11) |
설명 (Description)
이 연산자는 스레드 식별자 id의 텍스트 표현을 출력 스트림 ost에 써요. 두 스레드 식별자가 같으면 동일한 텍스트 표현을 갖고, 같지 않으면 서로 다른 표현을 가져요.
매개변수 (Parameters)
| 매개변수 | 설명 |
|---|---|
ost |
데이터를 삽입할 출력 스트림이에요. |
id |
출력할 스레드 식별자예요. |
반환값 (Return value)
ost를 그대로 반환해요.
예외 (Exceptions)
구현에서 정의한 예외를 던질 수 있어요.
예제 (Example)
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono;
int main()
{
std::thread t1([]{ std::this_thread::sleep_for(256ms); });
std::thread t2([]{ std::this_thread::sleep_for(512ms); });
std::clog << t1.get_id() << '\n' << t2.get_id() << '\n';
t1.join();
t2.join();
}
가능한 출력:
141592653589793
141421356237309