一种经典的死锁模型
来源:互联网 发布:2016年进出口贸易数据 编辑:程序博客网 时间:2024/06/05 18:18
#include <stack>#include <iostream>#include <set>#include <cstdio>#include <boost/thread.hpp>#include <boost/atomic.hpp>#include <boost/function.hpp>#include <boost/bind.hpp>using namespace std;using namespace boost;class Request;class Inventory{public: void add(Request *req) { mutex::scoped_lock lock(mutex_); requests_.insert(req); } void remove(Request *req) { mutex::scoped_lock lock(mutex_); requests_.erase(req); } void printAll();private: mutex mutex_; set<Request*> requests_;} g_inventory;class Request{public: void process() { mutex::scoped_lock lock(mutex_); g_inventory.add(this); } ~Request() { mutex::scoped_lock lock(mutex_); sleep(1); g_inventory.remove(this); } void print() { mutex::scoped_lock lock(mutex_); printf("this is request\n"); }private: mutex mutex_;};void Inventory::printAll(){ mutex::scoped_lock lock(mutex_); sleep(1); for(set<Request*>::const_iterator it=requests_.begin(); it!=requests_.end(); ++it) { (*it)->print(); } printf("Inventory::printAll() unlocked\n");}void threadFunc(){ Request *req=new Request(); req->process(); delete req;}int main(){ thread t(threadFunc); usleep(500*1000); g_inventory.printAll(); t.join();}
0 0
- 一种经典的死锁模型
- 简单实现一种经典的数据传输模型
- 死锁的经典例子
- 线程的一种解决死锁的方式
- 一种有效避免死锁的互斥锁设计
- 一种有效避免死锁的互斥锁设计
- linux死锁检测的一种思路
- gcd 发生死锁的一种情况
- 关于Java死锁的一种实现
- 一种Future模型的实现
- NlogN的经典模型
- 经典的光照模型
- 飘逸的python - 演示一种死锁的产生
- 解开硬盘逻辑死锁的一种有效方法
- 引起线程死锁的一种情况及解决方法
- Sql Server 2005 中解决死锁的一种方式
- 解开硬盘逻辑死锁的一种有效方法
- Sql Server 2005 中解决死锁的一种方式
- 关于MATLAB7.0与vc++6.0通过com接口实现混合编程
- BXP服务器冗余
- MYSQL入门学习之十一:触发器的基本操作
- 安装MongoDB
- 查看dll位数
- 一种经典的死锁模型
- 二人三足跑
- JAVA中clone方法详解
- HDU 3910 Liang Guo Sha
- MYSQL入门学习之十二:存储过程的基本操作
- C++中的字符串内涵
- MYSQL入门学习之十三:自定义函数的基本操作
- 【图论】最小环算法
- 专访曾宪杰:大型网站系统与Java中间件实践