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的选项 ...
随机推荐
- NeHe OpenGL lession 4
// lession4.c #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> ...
- SSAS 发布报错处理方法 Login failed for user 'NT Service\MSSQLServerOLAPService' 28000
Create login and grant access: Open up SQL Server Management Studio [login to the database engine]&g ...
- [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
介绍 在本文中我们将利用SSIS参数,变量 以及 Foreach Loop Container 从多个源动态导入数据. 开始前我们先下载一些数据.WeatherData_Dec08_Apr09.zip ...
- 前端CSS规范大全
一.文件规范 1.文件均归档至约定的目录中(具体要求以豆瓣的CSS规范为例进行讲解): 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用 ...
- Cloudera Manager、CDH零基础入门、线路指导 http://www.aboutyun.com/thread-9219-1-1.html (出处: about云开发)
Cloudera Manager.CDH零基础入门.线路指导http://www.aboutyun.com/thread-9219-1-1.html(出处: about云开发) 问题导读:1.什么是c ...
- js 乱码解决方案
如果网页编码规则是utf-8,即在网页头部中可以看到如下代码: 那么js文件中如果有中文字,调用输出时就会出现乱码.解决此个问题的办法如下: 即在引用javascript输出的地方加上charset= ...
- C++对C语言的非面向对象特性扩充(3)
今天要讲的是C++作用域运算符"::",强制类型转换的扩充,C++中相对于C中malloc和free函数的运算符new和delete,以及C++对C的一个重要扩充:引用(refer ...
- PHP学习笔记11-表单
处理GET请求 实现的功能是输入姓名后页面显示“Hello XXX” 创建html文件hello.html: <!DOCTYPE html> <html> <head l ...
- [LeetCode]题解(python):065-Valid Number
题目来源: https://leetcode.com/problems/valid-number/ 题意分析: 输入一个字符串,判断这个字符串表示的是不是一个有效的数字.比如: "0&quo ...
- jquery 动态增加的html元素,初始化设置在id或class上的事件无效
一般情况,我们会在页面初始化完成后对class定义一些全局事件,举个栗子: $(document).ready(function(){ $(".class").on("m ...