C++11多線程

https://github.com/forhappy/Cplusplus-Concurrency-In-Practiceios

https://en.cppreference.com/w/cpp/atomicgit

detach以後不能joingithub

terminate called after throwing an instance of 'std::system_error'
  what():  Invalid argument
Abort trap: 6
    std::thread t1(function_print);
    t1.detach();
    if(t1.joinable())
    {
        t1.join();
    }

關於類的多線程,顯然輸出是混亂的多線程

#include<iostream>
#include<thread>
void function_print()
{
    std::cout<<"www.bobhuang.xyz"<<std::endl;
}
class Fac
{
    public:
    void operator()()
    {
        for(int i=0;i<10;i++)
        std::cout<<"form t1:"<<i<<std::endl;
    }
};
int main()
{
    Fac fct;
    std::thread t1(fct);
    try
    {
        for(int i=0;i<10;i++)
        std::cout<<"form main:"<<i<<std::endl;
    }
    catch(...)
    {
        t1.join();
        throw;
    }
    t1.join();
    return 0;
}
相關文章
相關標籤/搜索