#include "../Mutex.h" #include "../Thread.h" #include #include #include class Request; class Inventory { public: Inventory() : requests_(new RequestList) { } void add(Request* req) { muduo::MutexLockGuard lock(mutex_); if (!requests_.unique()) { requests_.reset(new RequestList(*requests_)); printf("Inventory::add() copy the whole list\n"); } assert(requests_.unique()); requests_->insert(req); } void remove(Request* req) // __attribute__ ((noinline)) { muduo::MutexLockGuard lock(mutex_); if (!requests_.unique()) { requests_.reset(new RequestList(*requests_)); printf("Inventory::remove() copy the whole list\n"); } assert(requests_.unique()); requests_->erase(req); } void printAll() const; private: typedef std::set RequestList; typedef boost::shared_ptr RequestListPtr; RequestListPtr getData() const { muduo::MutexLockGuard lock(mutex_); return requests_; } mutable muduo::MutexLock mutex_; RequestListPtr requests_; }; Inventory g_inventory; class Request { public: Request() : x_(0) { } ~Request() __attribute__ ((noinline)) { muduo::MutexLockGuard lock(mutex_); x_ = -1; sleep(1); g_inventory.remove(this); } void process() // __attribute__ ((noinline)) { muduo::MutexLockGuard lock(mutex_); g_inventory.add(this); // ... } void print() const __attribute__ ((noinline)) { muduo::MutexLockGuard lock(mutex_); // ... printf("print Request %p x=%d\n", this, x_); } private: mutable muduo::MutexLock mutex_; int x_; }; void Inventory::printAll() const { RequestListPtr requests = getData(); sleep(1); for (std::set::const_iterator it = requests->begin(); it != requests->end(); ++it) { (*it)->print(); } } void threadFunc() { Request* req = new Request; req->process(); delete req; } int main() { muduo::Thread thread(threadFunc); thread.start(); usleep(500*1000); g_inventory.printAll(); thread.join(); }