利用boost库:
class rw_data{
private:
int m_x;
shared_mutex rw_mu;
public:
rw_data():m_x(0){}
void write(){
unique_lock<shared_mutex> ul(rw_mu);
++m_x;
}
void read(int* x){
shared_lock<shared_mutex> sl(rw_mu);
*x=m_x;
}
};
void writer(rw_data& d){
for(int i=0;i<20;++i){
this_thread::sleep_for(chrono::milliseconds(10));
d.write();
}
}
void reader(rw_data& d){
int x;
for(int i=0;i<10;++i){
this_thread::sleep_for(chrono::milliseconds(5));
d.read(&x);
mutex::scoped_lock lock(io_mu);
cout<<"reader:"<<x<<endl;
}
}
int main(){
rw_data d;
thread_group pool;
boost::function<void()> f=boost::bind(writer,boost::ref(d));
boost::function<void()> f2=boost::bind(reader,boost::ref(d));
pool.create_thread(f);
pool.create_thread(f);
pool.create_thread(f2);
pool.create_thread(f2);
pool.create_thread(f2);
pool.create_thread(f2);
pool.join_all();
std::system("pause");
return 0;
}