原文地址:http://www.cplusplus.com/reference/thread/thread/detach/
public member function
<thread>

std::thread::detach

void detach();
Detach thread

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>       // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
} int main()
{
std::cout << "Spawning and detaching 3 threads...\n";
std::thread (pause_thread,1).detach();
std::thread (pause_thread,2).detach();
std::thread (pause_thread,3).detach();
std::cout << "Done spawning threads.\n"; std::cout << "(the main thread will now pause for 5 seconds)\n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}

Output (after 5 seconds):

Spawning and detaching 3 threads...
Done spawning threads.
(the main thread will now pause for 5 seconds)
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
pause of 5 seconds ended

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)的更多相关文章

  1. c++并发编程之thread::join()和thread::detach()

    thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...

  2. 漫谈C++11 Thread库之原子操作

    我在之前一篇博文<漫谈C++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程 ...

  3. c++11 thread的学习

    http://www.cnblogs.com/wxquare/p/6736202.html 还没开始 留个链接 使用c++11 thread支持实现  一个生产者消费者模型 下面是一个生产者消费者问题 ...

  4. 漫谈c++11 Thread库之使写多线程程序

    c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...

  5. 并发编程: c++11 thread(Func, Args...)利用类成员函数创建线程

    c++11是VS2012后支持的新标准,为并发编程提供了方便的std::thread. 使用示例: #include <thread> void thread_func(int arg1, ...

  6. C++11 Thread多线程的学习心得与问题

    C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...

  7. c++11 Thread库写多线程程序

    一个简单的使用线程的Demo c++11提供了一个新的头文件<thread>提供了对线程函数的支持的声明(其他数据保护相关的声明放在其他的头文件中,暂时先从thread头文件入手吧),写一 ...

  8. c++11: <thread>学习

    <thread>头文件中包含thread类与this_thread命名空间,下面逐一介绍. thread类 1. 构造函数 (1)默认构造函数 thread() noexcept; 默认构 ...

  9. c++11 thread (目前我使用的ZThread库)

    目前为止(2014-11-30),GCC其实已经基本上完全支持C++11的所有功能了,事实上从GCC4.7之后,就支持了-std=c++11选项,在4.7版本之前,也开始支持-std=c++0x的选项 ...

随机推荐

  1. HDU 1870 愚人节的礼物

    题解:简单的数括号问题…… #include <cstdio> char s[1005]; int main(){ while(scanf("%s",s)!=EOF){ ...

  2. Oracle select into from 和 insert into select

    select into from SQLSERVER  创建表: select * into aaa from bbb Oracle 创建表: create table aaa as select * ...

  3. android 中文 api (71) —— BluetoothServerSocket[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothServerSocket,为Android蓝牙部分的章节翻译.服务器通讯套接字,与TCP ServerSocket类似.版本为 ...

  4. c 按输入的字母来输出对应效果

    输入一个大写字母,如 F,输出 比如: 输入:F 输出: F EFE DEFED CDEFEDC BCDEFEDCB ABCDEFEDCBA #include<stdio.h> int m ...

  5. jQuery的fancybox插件

  6. poj 2513

    http://poj.org/problem?id=2513 73348K        1438MS        C++        1614B解题思路:欧拉路的应用 要点 :1.判断连通性   ...

  7. Solr4.7从文件创建索引

    索引数据源并不会一定来自于数据库.XML.JSON.CSV这类结构化数据,很多时候也来自于PDF.word.html.word.MP3等这类非结构化数据,从这类非结构化数据创建索引,solr也给我们提 ...

  8. 细说UI线程和Windows消息队列(经典)

    在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...

  9. 复习知识点:XML解析数据,JOSN解析数据,GET请求数据,POST请求数据

    GDataXMLDocument-XML解析 #pragma mark - ======== Dom XML解析 - (IBAction)domParserActionXML_Document:(id ...

  10. poj 3691

    ac自动机+dp 自动机上的节点来作为状态 dp[i][j]表示长度为i状态为j至少需要转换多少个字符 #include <iostream> #include <cstdio> ...