物理核心线程数量
thread::hardware_concurrency()
静态成员函数,返回真正的可以并行的线程数量,在多核系统上,它可能是cpu的核心数量。
过多的线程数量会增加上下文切换的负担,减少性能。
开辟多线程
mem_fn() 函数,可以将成员函数转为 函数对象。
#include <thread>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void func_work(unsigned id)
{
    printf("[%d]hello,thread id = %d\n",id,this_thread::get_id());
}
int main()
{
    vector<thread> threads;
    for (unsigned i = 0; i < 20; i++)
    {
        threads.push_back(thread(func_work,i));
    }
    // mem_fn 可以将成员函数封装成函数对象
    // for_each(threads.begin(),threads.end(),mem_fn(&thread::join));
    for_each(threads.begin(),threads.end(),[=](thread &i){
        i.join();
    });
    
    exit(0);
}









