C++11this_threadget_id(9)


C++11this_threadget_id(9)

原文地址:http://www.cplusplus.com/reference/thread/this_thread/get_id/
function
<thread>

std::this_thread::get_id

thread::id get_id() noexcept;
Get thread id

Returns the thread id of the calling thread.

返回当前调用的线程ID。


This value uniquely identifies the thread.
该值独一地标识了一个线程。

Parameters

none


Return value

An object of member type thread::id that uniquely identifies the thread.
返回一个线程ID值独一地标识了一个线程。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// thread::get_id / this_thread::get_id
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono>         // std::chrono::seconds
 
std::thread::id main_thread_id = std::this_thread::get_id();
//设置为全局所以该ID是主线程的ID
void is_main_thread() {
  if ( main_thread_id == std::this_thread::get_id() )
    std::cout << "This is the main thread.\n";
  else
    std::cout << "This is not the main thread.\n";
}

int main() 
{
  is_main_thread();
  std::thread th (is_main_thread);
  th.join();
}

Output:

This is the main thread.
This is not the main thread.



Exception safety

No-throw guarantee: never throws exceptions.



—————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-4

于GDUT

——————————————————————————————————————————————————————————————————





发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注