原文地址: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. 【iOS开发-52】假设要模仿练习,怎样找到其它应用程序的icon、使用框架等资源?

    (1)在91助手等站点下载ipa格式的安装包.假设是安卓的应该是apk格式的.此处仅仅说iOS的.比方以下这个,下载到电脑. (2)然后,找到它,右击"归档",相当于解压. (3 ...

  2. android 基础学习图片六progross

    加载进度条应用

  3. VC获取屏幕分辨率大小

    //以下两个函数获取的是显示屏幕的大小,不包括任务栏等区域 int screenwidth=GetSystemMetrics(SM_CXFULLSCREEN); int screenheight=Ge ...

  4. OD调试篇3-小软件破解1

    OD调试篇3-小软件破解1 要求如下图该软件需要改5个地方,其中1.2是软件未注册而设定限定的添加个数,3.4.5是软件显示的一些未注册的信息. 一. 1.按1运行程序,添加用户添加第五个时出现提示, ...

  5. UNIX网络编程5 POSIX 消息队列

    <mqueue.h> mq_open mq_close mq_unlink mq_getattr/mq_setattr mq_send/mq_receive mq_notify sigwa ...

  6. open和fopen的区别:

    1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓冲区”依此读入接收的变量.执行 ...

  7. QT学习 之 对话框 (四) 字体对话框、消息对话框、文件对话框、进程对话框(超详细中文注释)

    QMessageBox类: 含有Question消息框.Information消息框.Warning消息框和Critical消息框等 通常有两种方式可以来创建标准消息对话框: 一种是采用“基于属性”的 ...

  8. Java学习之链表

    数据结构学了,java实现下 package com.gh.Link; /** * 链表的实现 * @author ganhang * */ public class Links { public s ...

  9. poj 3691

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

  10. HDU 1069 monkey an banana DP LIS

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64uDescription 一组研究人员正在 ...