首页 > 代码库 > C++11 thread::joinable(5)

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

  • if it was default-constructed.
    默认构造器构造的。
  • if it has been moved from (either constructing another thread object, or assigning to it).
    通过移动构造获得的。
  • if either of its members join or detach has been called.
    调用了join或者detach方法的。


例子:

#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;
}
Edit & Run


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

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






C++11 thread::joinable(5)