C++11 thread::joinable(5)

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

std::thread::joinable

bool joinable() const noexcept;
Check if joinable
Returns whether the thread object is joinable.

返回线程对象是否是joinable的。


thread object is joinable if it represents a thread of execution.

如果是一个正在执行的线程,那么它是joinable的。


thread object is not joinable in any of these cases:

下列任一情况都是非joinable

例子:

#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
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<<"n="<<n<<endl;
}
thread returnThread(){
	thread tt(show,10);	
	return tt;
}

int main()
{

	thread t(show,18);
	cout<<"t is joinable? "<<t.joinable()<<endl;
	

	thread t1(returnThread());
	cout<<"t1 is joinable? "<<t1.joinable()<<endl;
	

	thread t2(show,3);
	cout<<"t2 is joinable? "<<t2.joinable()<<endl;
	t2.join();
	cout<<"after t2.join(),t2 is joinable? "<<t2.joinable()<<endl;

	thread t3(show,5);
	cout<<"t3 is joinable? "<<t3.joinable()<<endl;
	t3.detach();
	cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl;

}
运行结果:


用GDB调试发现一个好神奇的东西。


运行完了

	cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl;

之后,居然像栈析解一样,好神奇阿,现在还不知道为什么呢。。
先保留着,下次解决。


Parameters

none

Return value

true if the thread is joinable.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// example for thread::joinable
#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void mythread() 
{
  // do stuff...
}
 
int main() 
{
  std::thread foo;
  std::thread bar(mythread);

  std::cout << "Joinable after construction:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << '\n';
  std::cout << "bar: " << bar.joinable() << '\n';

  if (foo.joinable()) foo.join();
  if (bar.joinable()) bar.join();

  std::cout << "Joinable after joining:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << '\n';
  std::cout << "bar: " << bar.joinable() << '\n';

  return 0;
}


Output (after 3 seconds):
Joinable after construction:
foo: false
bar: true
Joinable after joining:
foo: false
bar: false

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

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






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值