C++11 thread::joinable(5)
来源:互联网 发布:c语言 bmp 像素值读取 编辑:程序博客网 时间:2024/05/15 23:47
原文地址: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的。
如果是一个正在执行的线程,那么它是joinable的。
A 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
noneReturn value
true
if the thread is joinable.false
otherwise.Example
123456789101112131415161718192021222324252627
// 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: falsebar: trueJoinable after joining:foo: falsebar: 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 0
- C++11 thread::joinable(5)
- thread joinable
- Thread(C#)
- c thread
- thread.c
- 线程的状态--joinable、detached
- something about thread(c#)
- linux + thread + c (一)
- C#Thread的使用
- 【c++】线程thread类
- c++thread学习(1)
- C++/MFC-线程Thread
- C++:线程(std::thread)
- POSIX线程状态:Joinable和Detached
- POSIX线程状态:Joinable和Detached
- 线程的状态----joinable和detached
- Linux 多线程之Joinable和Detached
- 线程的状态----joinable和detached
- vc++实现word应用程序事件监听及响应
- C++实现文件简单加密与解密(读取文件中的字符,对字符转换,然后写入,完成加密,解密反之)
- http://movie.douban.com/doulist/12825932/
- svn相关
- 23 种设计模式
- C++11 thread::joinable(5)
- C/C++ 函数原型(或函数声明)的相同点与不同点
- 找回回收站中误删的文件方法
- IOS 7.0调用Alert view 添加textfield
- cname 记录
- iOS开发中KVO的内部实现
- 程序员生存定律--目录
- Java通过JNI调用C的一个小DEMO
- 数据库关系图