设为首页 加入收藏

TOP

C++11 thread::detach(2)
2015-07-20 17:45:42 来源: 作者: 【 】 浏览:1
Tags:thread::detach
?

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 
   
    
#include 
    
      #include 
     
       #include 
      
        #include 
       
         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<
        
         0){ fout<<1currentThread is <
         
          运行截图:
          

?

\

\

可以看出,当进程结束的时候,即便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

?
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2762 Going from u to v or f.. 下一篇BNUOJ 34990 北京邀请赛最后一题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何利用Python做数 (2025-12-24 23:48:36)
·如何使用python进行 (2025-12-24 23:48:34)
·python 爬虫入门该怎 (2025-12-24 23:48:31)
·Java 实现多个大文件 (2025-12-24 23:22:00)
·Java多线程编程在工 (2025-12-24 23:21:56)