C++11threadget_id(3)


C++11threadget_id(3)

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

std::thread::get_id

id get_id() const noexcept;
Get thread id

Returns the thread id.

返回线程ID

If the thread object is joinable,
the function returns a value that uniquely identifies the thread.

如果线程对象是joinable的,那么将会返回一个独一无二的线程标识。(这里我试了几个例子,还是搞不太明白,还是先保留该问题。看最后一个的例子)

例子:

#include <iostream>
#include <thread>
using namespace std;
void show(int n){
		cout<<"show1:get_id() is "<<this_thread::get_id()<<",Now n is "<<n<<endl;
}
void show2(int n){
	
		cout<<"show2:get_id() is "<<this_thread::get_id()<<",Now n is "<<n<<endl;
}
int main()
{
	cout<<"main starts"<<endl;
	cout<<"mainThread is "<<this_thread::get_id()<<endl;
	thread t2(show,10);
	cout<<"t2 is "<<t2.get_id()<<endl;
	t2.join();
	cout<<"after t2.join(),t2 is "<<t2.get_id()<<endl;
	
	thread t3(show2,99);
	cout<<"t3 is "<<t3.get_id()<<endl;
	t3.detach();
	cout<<"after t3.detach(),t3 is "<<t3.get_id()<<endl;
	
	cout<<"main complete!"<<endl;
		
	
}

运行及GDB调试截图:



可以看到,两个子线程都在main还没结束的时候就已经exited了!并且第一个子线程在第二个子线程还没有构造的时候就已经exited了。

这里的返回值都是一样,换个更好点的例子:

#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
//delay(n) 延时n秒  
void delay(double sec)  
{  
    time_t start_time, cur_time; // 变量声明  
    time(&start_time);  
    do {  
        time(&cur_time);  
        }while((cur_time - start_time) < sec );  
}; 
void show(int n){
		cout<<"show1:get_id() is "<<this_thread::get_id()<<",Now n is "<<n<<endl;
		delay(3);
		cout<<"show1 complete!"<<endl;
}
void show2(int n){
	
		cout<<"show2:get_id() is "<<this_thread::get_id()<<",Now n is "<<n<<endl;
		delay(3);
		cout<<"show2 complete!"<<endl;
}
int main()
{
	cout<<"main starts"<<endl;
	cout<<"mainThread is "<<this_thread::get_id()<<endl;

	thread t3(show2,99);
	cout<<"t3 is "<<t3.get_id()<<endl;
	t3.detach();
	cout<<"after t3.detach(),t3 is "<<t3.get_id()<<endl;


	thread t2(show,10);
	cout<<"t2 is "<<t2.get_id()<<endl;
	t2.join();
	cout<<"after t2.join(),t2 is "<<t2.get_id()<<endl;


	cout<<"main complete!"<<endl;
		
	
}

运行截图:


可以看到show1和t1的ID是一样的,show2和t3的ID是一样的。(这里显示的有点混乱)

If the thread object is not joinable,
the function returns a default-constructed object of member type thread::id.
如果线程对象不是joinable,那么将会返回默认构造时的ID。

Parameters

none


Return value

An object of member type thread::id that uniquely identifies the thread (if joinable),
or default-constructed (if not joinable)

返回一个线程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();

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.



根据example修改的例子:

#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
std::thread::id main_thread_id = std::this_thread::get_id();
void show(int n){
	cout<<endl<<"main_thread_id="<<main_thread_id<<endl;
	cout<<"thi_thread::get_id="<<this_thread::get_id()<<endl;
	cout<<"SHOW complete!"<<endl;
}
int main()
{
	cout<<endl<<"Main main_thread_id="<<main_thread_id<<endl;
	cout<<"Main this_thread::get_id= "<<this_thread::get_id()<<endl;

	show(888);	

	thread t3(show,99);
	cout<<"t3 is "<<t3.get_id()<<endl;
	t3.detach();


	thread t2(show,10);
	cout<<"t2 is "<<t2.get_id()<<endl;
	t2.join();

}

结果:


令我疑惑的是在开头那个全局设置

std::thread::id main_thread_id = std::this_thread::get_id();

和在函数中的

this_thread::get_id()

有什么区别?难道是全局的那个肯定是main的,这样就说的过去了,因为和main的返回值是一样的

个人觉得应该是joinable是我理解有些误差。

Data races

The object is accessed.


Exception safety

No-throw guarantee: never throws exceptions.


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

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

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

author:天下无双

Email:coderguang@gmail.com

2014-9-4

于GDUT

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



发表回复

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