MutualDeadLock.cc revision d3811de4
1d3811de4SShuo Chen#include "../Mutex.h" 2d3811de4SShuo Chen#include "../Thread.h" 3d3811de4SShuo Chen#include <set> 4d3811de4SShuo Chen#include <stdio.h> 5d3811de4SShuo Chen 6d3811de4SShuo Chenclass Request; 7d3811de4SShuo Chen 8d3811de4SShuo Chenclass Inventory 9d3811de4SShuo Chen{ 10d3811de4SShuo Chen public: 11d3811de4SShuo Chen void add(Request* req) 12d3811de4SShuo Chen { 13d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 14d3811de4SShuo Chen requests_.insert(req); 15d3811de4SShuo Chen } 16d3811de4SShuo Chen 17d3811de4SShuo Chen void remove(Request* req) __attribute__ ((noinline)) 18d3811de4SShuo Chen { 19d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 20d3811de4SShuo Chen requests_.erase(req); 21d3811de4SShuo Chen } 22d3811de4SShuo Chen 23d3811de4SShuo Chen void printAll() const; 24d3811de4SShuo Chen 25d3811de4SShuo Chen private: 26d3811de4SShuo Chen mutable muduo::MutexLock mutex_; 27d3811de4SShuo Chen std::set<Request*> requests_; 28d3811de4SShuo Chen}; 29d3811de4SShuo Chen 30d3811de4SShuo ChenInventory g_inventory; 31d3811de4SShuo Chen 32d3811de4SShuo Chenclass Request 33d3811de4SShuo Chen{ 34d3811de4SShuo Chen public: 35d3811de4SShuo Chen void process() // __attribute__ ((noinline)) 36d3811de4SShuo Chen { 37d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 38d3811de4SShuo Chen g_inventory.add(this); 39d3811de4SShuo Chen // ... 40d3811de4SShuo Chen } 41d3811de4SShuo Chen 42d3811de4SShuo Chen ~Request() __attribute__ ((noinline)) 43d3811de4SShuo Chen { 44d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 45d3811de4SShuo Chen sleep(1); 46d3811de4SShuo Chen g_inventory.remove(this); 47d3811de4SShuo Chen } 48d3811de4SShuo Chen 49d3811de4SShuo Chen void print() const __attribute__ ((noinline)) 50d3811de4SShuo Chen { 51d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 52d3811de4SShuo Chen // ... 53d3811de4SShuo Chen } 54d3811de4SShuo Chen 55d3811de4SShuo Chen private: 56d3811de4SShuo Chen mutable muduo::MutexLock mutex_; 57d3811de4SShuo Chen}; 58d3811de4SShuo Chen 59d3811de4SShuo Chenvoid Inventory::printAll() const 60d3811de4SShuo Chen{ 61d3811de4SShuo Chen muduo::MutexLockGuard lock(mutex_); 62d3811de4SShuo Chen sleep(1); 63d3811de4SShuo Chen for (std::set<Request*>::const_iterator it = requests_.begin(); 64d3811de4SShuo Chen it != requests_.end(); 65d3811de4SShuo Chen ++it) 66d3811de4SShuo Chen { 67d3811de4SShuo Chen (*it)->print(); 68d3811de4SShuo Chen } 69d3811de4SShuo Chen printf("Inventory::printAll() unlocked\n"); 70d3811de4SShuo Chen} 71d3811de4SShuo Chen 72d3811de4SShuo Chenvoid threadFunc() 73d3811de4SShuo Chen{ 74d3811de4SShuo Chen Request* req = new Request; 75d3811de4SShuo Chen req->process(); 76d3811de4SShuo Chen delete req; 77d3811de4SShuo Chen} 78d3811de4SShuo Chen 79d3811de4SShuo Chenint main() 80d3811de4SShuo Chen{ 81d3811de4SShuo Chen muduo::Thread thread(threadFunc); 82d3811de4SShuo Chen thread.start(); 83d3811de4SShuo Chen usleep(500); 84d3811de4SShuo Chen g_inventory.printAll(); 85d3811de4SShuo Chen thread.join(); 86d3811de4SShuo Chen} 87