C++11 thread::detach(2)

原文地址: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 typeerror conditiondescription
system_errorerrc::invalid_argumentThe thread object is not joinable
system_errorerrc::no_such_processThe thread object is not valid


—————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-3

于GDUT

——————————————————————————————————————————————————————————————————






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值