zoukankan      html  css  js  c++  java
  • #include <thread>

    1 detach

    脱离当前主线程,自由执行,乱序;

    2 join()

    等待模式,执行完再执行下一个

    3 std::this_thread::get_id() 

    获取当前线程编号

    4 std::thread::hardware_concurrency()

    检测CPU有多少个核心

    1 detach

    脱离当前主线程,自由执行,乱序;

    2 join()

    等待模式,执行完再执行下一个

     1 #include <iostream>
     2 #include <thread>
     3 
     4 void run(int num)
     5 {
     6     std::cout << "hello world" << num << std::endl;
     7 }
     8 
     9 void main()
    10 {
    11     std::thread *p[10];
    12 
    13     for (int i = 0; i < 10; i++)
    14     {
    15         p[i] = new std::thread(run, i);//循环创建线程
    16         //p[i]->join();//等待模式,执行完再执行下一个
    17         p[i]->detach();//脱离当前主线程,自由执行,乱序;
    18     }
    19 
    20     system("pause");
    21 }

    1 join()

    等待模式,执行完再执行下一个

    2 std::this_thread::get_id() 

    获取当前线程编号

    3 std::thread::hardware_concurrency()

    检测CPU有多少个核心

     1 #include <iostream>
     2 #include <thread>
     3 #include <windows.h>
     4 
     5 void msg()
     6 {
     7     MessageBoxA(0, "对话框内容", "对话框标题", 0);//弹出对话框
     8 }
     9 
    10 void main()
    11 {
    12     auto n = std::thread::hardware_concurrency();//检测CPU有多少个核心
    13     std::cout << n << std::endl;
    14 
    15     std::cout << "thread=" << std::this_thread::get_id() << std::endl;//获取当前线程编号
    16 
    17     std::thread thread1(msg);//创建多线程
    18     std::thread thread2(msg);
    19 
    20     thread1.join();//开始执行,同时弹出2个对话框
    21     thread2.join();
    22 
    23     system("pause");
    24 }

    std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread *

    threads.push_back(new std::thread(msg));//创建线程,并添加到数组

     1 #include <iostream>
     2 #include <thread>
     3 #include <vector>
     4 #include <windows.h>
     5 
     6 void msg()
     7 {
     8     MessageBoxA(0, "对话框内容", "对话框标题", 0);//弹出对话框
     9 }
    10 
    11 void main()
    12 {
    13     std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread *
    14 
    15     for (int i = 0; i < 10; i++)
    16     {
    17         threads.push_back(new std::thread(msg));//创建线程,并添加到数组
    18     }
    19 
    20     for (auto th : threads)//遍历数组
    21     {
    22         th->join();//执行数组中的线程
    23     }
    24 
    25     system("pause");
    26 }

    threads.push_back(new std::thread(msgA, i));//创建线程,并添加到数组,传入参数i,进行通信

     1 #include <iostream>
     2 #include <thread>
     3 #include <vector>
     4 #include <windows.h>
     5 
     6 void msgA(int num)
     7 {
     8     std::cout << std::this_thread::get_id() << " num=" << num << std::endl;//获取当前线程编号
     9     MessageBoxA(0, "对话框内容", "对话框标题", 0);//弹出对话框
    10 }
    11 
    12 void main()
    13 {
    14     std::vector<std::thread *>threads;//创建一个数组,数组的元素数据类型是std::thread *
    15     
    16     for (int i = 0; i < 10; i++)
    17     {
    18         threads.push_back(new std::thread(msgA, i));//创建线程,并添加到数组,传入参数i,进行通信
    19     }
    20 
    21     for (auto th : threads)//遍历数组
    22     {
    23         th->join();//执行数组中的线程
    24     }
    25 
    26     system("pause");
    27 }
  • 相关阅读:
    Linux下部署SSH登录时的二次身份验证环境记录(利用Google Authenticator)
    Linux下DNS简单部署(主从域名服务器)
    MySQL 读写分离方案-MySQL Proxy环境部署记录
    Linux系统本地yum源环境配置记录
    Swarm基于多主机容器网络 (overlay networks )
    Docker管理工具
    Linux下相关性能指标数据采集
    Docker网络解决方案
    Java的接口和抽象类
    Java 8 lambda表达式示例
  • 原文地址:https://www.cnblogs.com/denggelin/p/5616213.html
Copyright © 2011-2022 走看看