您的位置:首页 > 编程语言 > C语言/C++

[置顶] c++多线程(多线程处理vector)

2018-03-03 15:18 239 查看
常规定义:

#include <thread>
#include<iostream>
#include<windows.h>
#include<vector>

using namespace std;
using namespace
9ce7
std::this_thread;

void msg()
{
MessageBoxA(0, "12345", "678910", 0);
}
void msgA(int num)
{
std::cout << get_id() << "  num=   " << num << std::endl;
}

void main1()
{
// thread::hardware_concurrency线程
auto n = thread::hardware_concurrency();
std::cout << n << std::endl;//n的值就是cpu的线程
//获取当前线程编号
std::cout << "thread=" << get_id() << std::endl;
thread thread1(msg);//创建多线程
thread thread2(msg);
thread1.join();//开始执行
thread2.join();
std::cin.get();
}

void main2()
{

vector<thread *> threads;
for (int i = 0; i < 10; i++)
{
threads.push_back(new thread(msg));//创建线程
}

for (auto th : threads)
{
th->join();
}

std::cin.get();

}
void main()
{
vector<thread *> threads;
for (int i = 0; i < 10; i++)
{
threads.push_back(new thread(msgA,i));//创建线程
}

for (auto th : threads)
{
th->join();
}
std::cin.get();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: