首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁Boost:共享锁”︁的源代码
←
Boost:共享锁
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
=示例代码= <source lang="cpp"> #include <iostream> #include <boost/thread.hpp> #include <windows.h> int sum = 0; boost::shared_mutex mu; int write(int c){ for (int i = 0; i < c; i++){ boost::unique_lock<boost::shared_mutex> lock(mu); sum++; cout << "=>" << sum << endl; } return sum; } void read(int nCount){ for (int i = 0; i < nCount; i++){ boost::shared_lock<boost::shared_mutex> lock(mu); cout << "<=" << sum << endl; } } int main(int argc, char* argv[]) { boost::thread t1(write, 100); boost::thread t2(read, 100); boost::thread t3(read, 100); t1.join(); t2.join(); t3.join(); system("pause"); return 1; } </source> =代码分析= 如果屏蔽掉上述代码两处加锁的地方,会出现什么情况? *第一次 [[Image:Test_Thread_ts1.png|600px]] *第二次 [[Image:Test_Thread_ts2.png|600px]] [[Category:Programe]]
返回
Boost:共享锁
。