15,473
社区成员




// 运行后可以看到,write_process中输出到console的内容是有序的,而且在begin of write _process和end of write_process中不会有
// 其他信息,而read_process中输出到console的内容是杂乱无章的,这就模拟了single-write/multi-read
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/shared_mutex.hpp>
using namespace std;
using namespace boost;
boost::shared_mutex shr_mutex;
void write_process() {
shr_mutex.lock();
cout << "begin of write_process" << endl;
cout << "end of write_process" << endl;
shr_mutex.unlock();
}
void read_process() {
shr_mutex.lock_shared();
cout << "begin of read_process" << endl;
cout << "end of read_process" << endl;
shr_mutex.unlock_shared();
}
int main() {
thread_group threads;
for (int i = 0; i < 10; ++ i) {
threads.create_thread(&write_process);
threads.create_thread(&read_process);
}
threads.join_all();
::system("PAUSE");
return 0;
}