https://github.com/forhappy/Cplusplus-Concurrency-In-Practice
https://en.cppreference.com/w/cpp/atomic
detach之后不能join
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; }