名前空間
変種
操作

std::this_thread::get_id

提供: cppreference.com
< cpp‎ | thread
 
 
スレッドサポートライブラリ
スレッド
(C++11)
(C++20)
(C++20)
this_thread 名前空間
get_id
(C++11)
(C++11)
(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)
 
ヘッダ <thread> で定義
std::thread::id get_id() noexcept;
(C++11以上)

現在のスレッドの id を返します。

目次

[編集] 引数

(なし)

[編集] 戻り値

現在のスレッドの id

[編集]

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
 
std::mutex g_display_mutex;
 
void foo()
{
    std::thread::id this_id = std::this_thread::get_id();
 
    g_display_mutex.lock();
    std::cout << "thread " << this_id << " sleeping...\n";
    g_display_mutex.unlock();
 
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::thread t1(foo);
    std::thread t2(foo);
 
    t1.join();
    t2.join();
}

出力例:

thread 0x2384b312 sleeping...
thread 0x228a10fc sleeping...

[編集] 関連項目

スレッドの id を返します
(std::threadのパブリックメンバ関数) [edit]
thrd_currentC言語リファレンス