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的选项 ...
随机推荐
- ThinkPHP - 事务操作
/* * 添加酒店和房型 * */ public function insertAll($arr_hotel=array(),$arr_room=array()){ $model = new Mode ...
- BZOJ 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝( dp )
先按时间排序( 开始结束都可以 ) , 然后 dp( i ) = max( dp( i ) , dp( j ) + 1 ) ( j < i && 节日 j 结束时间在节日 i 开 ...
- The Power of Reading——英语学习小技巧之七
This method is "The Power of Reading" and it comes from an article by Dr.Stephen Krashen. ...
- RSA, ACS5.X 集成配置
目的是RSA和ACS集成,ACS作为RADIUS服务器提供二次验证服务. ①配置RSA SecurID Token Servers 按照如下网址配置: http://www.cisco.com/c ...
- JDBC_获取数据库连接
<span style="font-size:24px;">package src.com.jdbc.java; import java.io.IOException; ...
- 操作数组的工具类Arrays
Java提供的Arrays类里包含一些static修饰的方法可以直接操作数组. int binarySearch(type[] a, type key)使用二分法查询key元素值在a数组中出现的索引, ...
- Windows Phone 8初学者开发—第6部分:设置应用程序的样式
原文 Windows Phone 8初学者开发—第6部分:设置应用程序的样式 Source Code: http://aka.ms/absbeginnerdevwp8 PDF Version: ht ...
- python and 和 or
在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样.但是它们并不返回布尔值,而是返回它们实际进行比较的值之一. 例 4.15. and 介绍 >>> 'a' a ...
- 解决安装oracle后系统变慢问题
Oracle数据库是一个很占资源的软件,光一个实例服务所占内存,根据其安装时分配的内存就至少要达到256MB以上,再加上其他附属服务,光内存就要占用物理内存的400M左右,虚拟内存也会有等值或更高的损 ...
- layer 的常用属性
layer的各种属性代码示例: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading ...