std::thread::native_handle
From cppreference.com
| native_handle_type native_handle(); |
(C++11以降) (常に存在するとは限りません) |
|
実装定義の基になるスレッドハンドルを返します。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
スレッドを表す、実装定義のハンドル型。
[編集] 例外
実装定義の例外をスローする場合があります。
[編集] 例
POSIXシステムでC++スレッドのリアルタイムスケジューリングを有効にするためにnative_handleを使用します。
このコードを実行
#include <chrono> #include <cstring> #include <iostream> #include <mutex> #include <pthread.h> #include <thread> std::mutex iomutex; void f(int num) { std::this_thread::sleep_for(std::chrono::seconds(1)); sched_param sch; int policy; pthread_getschedparam(pthread_self(), &policy, &sch); std::lock_guard<std::mutex> lk(iomutex); std::cout << "Thread " << num << " is executing at priority " << sch.sched_priority << '\n'; } int main() { std::thread t1(f, 1), t2(f, 2); sched_param sch; int policy; pthread_getschedparam(t1.native_handle(), &policy, &sch); sch.sched_priority = 20; if (pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n'; t1.join(); t2.join(); }
出力
Thread 2 is executing at priority 0 Thread 1 is executing at priority 20