我有使用boost线程的以下简单程序,在c ++ 0X中执行相同操作所需的更改
#include<iostream>
#include<boost/thread/thread.hpp>
boost::mutex mutex;
struct count
{
count(int i): id(i){}
void operator()()
{
boost::mutex::scoped_lock lk(mutex);
for(int i = 0 ; i < 10000 ; i++)
{
std::cout<<"Thread "<<id<<"has been called "<<i<<" Times"<<std::endl;
}
}
private:
int id;
};
int main()
{
boost::thread thr1(count(1));
boost::thread thr2(count(2));
boost::thread thr3(count(3));
thr1.join();
thr2.join();
thr3.join();
return 0;
}
答案 0 :(得分:2)
没有改变可言......
#include <iostream>
#include <thread>
std::mutex mutex;
struct count {
count(int i): id(i){}
void operator()()
{
std::lock_guard<std::mutex> lk(mutex); // this seems rather silly...
for(int i = 0 ; i < 10000 ; ++i)
std::cout << "Thread " << id << "has been called " << i
<< " Times" << std::endl;
}
private:
int id;
};
int main()
{
std::thread thr1(count(1));
std::thread thr2(count(2));
std::thread thr3(count(3));
thr1.join();
thr2.join();
thr3.join();
}