`
阿尔萨斯
  • 浏览: 4249851 次
社区版块
存档分类
最新评论

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 thethreadobject isjoinable.

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


Athreadobject isjoinableif it represents a thread of execution.

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


Athreadobject isnot joinablein 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

trueif the thread isjoinable.
falseotherwise.

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

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






分享到:
评论

相关推荐

    详解C++ thread用法总结

    C++11中加入了&lt;thread&gt;头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。应用C++11中的std::thread便于...

    多线程与智能指针.pdf

    C++11线程 #include &lt;thread&gt; void task(int i) { cout &lt;&lt; "task:" ; } thread t1(task,100); //等待线程结束再继续执⾏ t1.join(); POSIX线程 POSIX 可移植操作系统接⼝,标准定义了操作系统应该为应⽤程序提供的...

    jointable:使用任意键连接表对-matlab开发

    用法:joinable(tab1,tab2,keys1,keys2,fillval) JOINTABLE 的工作方式很像 SQL 连接,其中连接的每一行都对应于表中具有匹配键的行。 与不匹配的键对应的位置用指定的值填充。 例子: Jointable([1;2;3],[4;5],{...

    linux多线程设计及示例

    一个线程或者是可汇合的(joinable,缺省值),或者是脱离的(detached)。当一个可汇合的线程终止时,它的线程ID和退出状态将留到另一个线程对它调用pthread_join。脱离线程却象守护进程:当它们终止的时,所有相关...

    分离状态创建线程

    在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached)。一个可结合的线程能够被其他线程收回其资源和杀死;在被其他线程回收之前,它的存储器资源(如栈)是不释放的。相反,一个分离的线程是...

    scpfw-开源

    该软件包包含原始的scpfw(用于教授CP的“框架”),JISIMONEK(SIMONEK模型的Java实现)和SISONEK(用于研究Joinable Schedules的Java环境)。

Global site tag (gtag.js) - Google Analytics