?
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
|