- thread[meta header]
- std[meta namespace]
- jthread[meta class]
- function[meta id-type]
- cpp20[meta cpp]
[[nodiscard]] native_handle_type native_handle(); // (1) C++20スレッドのハンドルを取得する
この関数は、実装依存のスレッドのハンドルを返す。
- Unix系環境におけるlibstdc++とlibc++では、
pthread_tを表す。 - Visual C++では、Windowsのスレッドハンドル
HANDLEを表す。ただし、native_handle_typeはvoid*型の別名である。HANDLEもvoid*型の別名であり、別途<windows.h>または<wtypes.h>をインクルードするとHANDLE型が使用できる。
ハンドル型に対する操作は汎用的ではないため、環境依存のプログラミングが必要な場合に使用する。
実装依存のスレッドハンドル
#include <thread>
#include <iostream>
#include <windows.h>
void func() {
std::cout << "func" << std::endl;
}
int main() {
std::jthread t{func};
SetThreadPriority(t.native_handle(), THREAD_PRIORITY_IDLE);
t.join();
}- native_handle()[color ff0000]
func
- C++20
- Clang:
- GCC: 10.2.0
- Visual C++: ??