C++11 thread::detach(2)
std::thread::detach
void detach();
Detaches the thread represented by the object from the calling thread, allowing them to execute independently from each other.
将本线程从调用线程中分离出来,同意本线程独立运行。(可是当主进程结束的时候,即便是detach()出去的子线程无论有没有完毕都会被强制杀死)
样例:
#include <iostream>
#include <thread>
#include <vector>
#include <ctime>
#include <fstream>
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){
ofstream fout("detach.txt");
if(!fout.is_open())
cout<<"open failed!"<<endl;
while(n>0){
fout<<"1currentThread is "<<pthread_self()<<",Now n is "<<n<<endl;
delay(0.2);
n--;
}
fout<<"ok"<<endl;
fout.close();
}
int main()
{
cout<<"main starts"<<endl;
thread t(show,100);
t.detach();
delay(1);
cout<<"main complete!"<<endl;
}
执行截图:
能够看出,当进程结束的时候,即便detach没有完毕任务也会被强制杀死。
Both threads continue without blocking nor synchronizing in any way. Note that when either one ends execution, its resources are released.
两个线程不会阻塞也不会同步,注意他们任一一个结束的时候,所持有的资源将会被释放。
After a call to this function, the thread object becomes non-joinable and
can be destroyed safely.
调用该方法后,该线程对象变得不可连接以及能够安全地销毁。
样例:
Parameters
none
Return value
none
Example
|
|
Output (after 5 seconds):
Spawning and detaching 3 threads... |
Data races
The object is modified.
Exception safety
Basic guarantee: if an exception is thrown by this member function, the thread object is
left in a valid state.
If the call fails, a system_error exception is thrown:
| exception type | error condition | description |
|---|---|---|
| system_error | errc::invalid_argument | The thread object is not joinable |
| system_error | errc::no_such_process | The thread object is not valid |
—————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-3
于GDUT
——————————————————————————————————————————————————————————————————
C++11 thread::detach(2)的更多相关文章
- c++并发编程之thread::join()和thread::detach()
thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...
- 漫谈C++11 Thread库之原子操作
我在之前一篇博文<漫谈C++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程 ...
- c++11 thread的学习
http://www.cnblogs.com/wxquare/p/6736202.html 还没开始 留个链接 使用c++11 thread支持实现 一个生产者消费者模型 下面是一个生产者消费者问题 ...
- 漫谈c++11 Thread库之使写多线程程序
c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...
- 并发编程: c++11 thread(Func, Args...)利用类成员函数创建线程
c++11是VS2012后支持的新标准,为并发编程提供了方便的std::thread. 使用示例: #include <thread> void thread_func(int arg1, ...
- C++11 Thread多线程的学习心得与问题
C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...
- c++11 Thread库写多线程程序
一个简单的使用线程的Demo c++11提供了一个新的头文件<thread>提供了对线程函数的支持的声明(其他数据保护相关的声明放在其他的头文件中,暂时先从thread头文件入手吧),写一 ...
- c++11: <thread>学习
<thread>头文件中包含thread类与this_thread命名空间,下面逐一介绍. thread类 1. 构造函数 (1)默认构造函数 thread() noexcept; 默认构 ...
- c++11 thread (目前我使用的ZThread库)
目前为止(2014-11-30),GCC其实已经基本上完全支持C++11的所有功能了,事实上从GCC4.7之后,就支持了-std=c++11选项,在4.7版本之前,也开始支持-std=c++0x的选项 ...
随机推荐
- golang(2):beego 环境搭建
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/46695513 转载请一定注明出处. 1,关于beego beego是一个用Go开 ...
- 在C#中使用 Win32 和其他库
C# 用户经常提出两个问题:“我为什么要另外编写代码来使用内置于 Windows® 中的功能?在框架中为什么没有相应的内容可以为我完成这一任务?”当框架小组构建他们的 .NET 部分时,他们评估了为使 ...
- ie调试控制台
function initConsole() { var console = document.createElement('div'); console.id = 'errorConsole'; d ...
- iOS推送证书p12转成pem
首先你需要导出p12格式的证书,具体操作请参考如下: 其次你就可以通过在控制台输入如下命令即可转换: openssl pkcs12 -in 你导出的p12证书 -out 你要转换的pem证书 -nod ...
- 「C」 函数、运算、流程控制
一.函数 (一)什么是函数 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”. (二)函数的定义 目的:将一个常用的功能封装起来,方 ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- virtual host
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.php100.com DocumentRoot "G:/w ...
- jquery 几点注意事项
jquery触发hover事件 使用 mouseenter/mouseleave/mouseover/mouseout
- cocos2dx进阶学习之CCSpriteBatchNode
继承关系 CCSpriteBatchNode -> CCNode, CCTextureProtocol 成员变量 inline CCTextureAtlas* getTextureAtlas(v ...