C++ 11 查看硬件线程个数等信息

来源:互联网 发布:c语言常考题 编辑:程序博客网 时间:2024/06/03 23:04
#include <iostream>#include <thread>#include <pthread.h>#include <mutex>using namespace std;mutex mtx;void showinfo(){lock_guard<mutex> lk(mtx);        cout<<"hardware thread is "<<thread::hardware_concurrency()<<endl;      cout<<"thread id is "<<pthread_self()<<endl;        cout<<"thread id is "<<this_thread::get_id()<<endl;}int main(){        thread t1(showinfo);{lock_guard<mutex> lk(mtx);cout<<"thread native handle is "<<t1.native_handle()<<endl;}        t1.join();        return 0;}


在linux下,线程原始句柄就是线程的ID。

在windows下(需将代码中与pthread相关的部分去掉),得到的线程ID和线程原始句柄不一样。

PS:在mingw下,信息与linux一致。

0 0
原创粉丝点击