#include <memory>
#include <thread>
#include <vector>
#include <mutex>
#include <atomic>
#include <iostream>
class threadClass{
public:
void run(int num){
while(num>0){
auto tt = std::make_shared<std::thread>(&threadClass::test,this);
threads_.push_back(tt);
--num;
}
for(auto tt:threads_){
tt->join();
}
threads_.clear();
}
private:
void test(){
int val = val_++;
val_lock_.lock();
std::cout << val << std::endl;
val_lock_.unlock();
}
private:
std::atomic<int> val_{0};
std::vector<std::shared_ptr<std::thread> > threads_;
std::mutex val_lock_;
};
int main()
{
threadClass tt;
tt.run(10);
tt.run(10);
return 0;
}
[cpp] c++11 member function thread test
猜你喜欢
转载自blog.csdn.net/adream307/article/details/80589214
今日推荐
周排行