zoukankan      html  css  js  c++  java
  • std::detach()用法

     1 #include <chrono>
     2 #include <thread>
     3 
     4 void independentThread()
     5 {
     6   std::cout << "Starting concurrent thread.
    ";
     7   std::this_thread::sleep_for(std::chrono::seconds(2));
     8   std::cout << "Exiting concurrent thread.
    ";
     9 }
    10 
    11 void threadCaller()
    12 {
    13   std::cout << "Starting thread caller.
    ";
    14   std::thread t(independentThread);
    15   t.detach();
    16   std::this_thread::sleep_for(std::chrono::seconds(1));
    17   std::cout << "Exiting thread caller.
    ";
    18 }
    19 
    20 int main()
    21 {
    22   threadCaller();
    23   std::this_thread::sleep_for(std::chrono::seconds(5));
    24 }

    输出:

     1 #include <iostream>
     2 #include <chrono>
     3 #include <thread>
     4 using namespace std;
     5 
     6 void independentThread()
     7 {
     8   std::cout << "Starting concurrent thread.
    ";
     9   std::this_thread::sleep_for(std::chrono::seconds(10));
    10   std::cout << "Exiting concurrent thread.
    ";
    11 }
    12 
    13 void threadCaller()
    14 {
    15   std::cout << "Starting thread caller.
    ";
    16   std::thread t(independentThread);
    17   t.detach();
    18   std::this_thread::sleep_for(std::chrono::seconds(1));
    19   std::cout << "Exiting thread caller.
    ";
    20 }
    21 
    22 int main()
    23 {
    24   threadCaller();
    25   std::this_thread::sleep_for(std::chrono::seconds(1));
    26 }

    输出:

  • 相关阅读:
    lvs实现故障转移(backup)
    shell计算
    CEGUI 聊天对话框
    SetRenderState 设置渲染状态【转】
    MFC 弹出对话框
    DrawIndexedPrimitive函数的详细解释【转】
    IDirect3DDevice9::Clear 【转】
    manifest 文件错误
    D3DXMatrixLookAtLH 【转】
    D3D中的网格(Mesh)
  • 原文地址:https://www.cnblogs.com/sunbines/p/14947812.html
Copyright © 2011-2022 走看看