Skip to content
Browse files

add two deadlock examples.

  • Loading branch information...
1 parent b640675 commit d3811de41c03a443f1e63423868b03dd62e7cee3 @chenshuo committed
Showing with 123 additions and 6 deletions.
  1. +9 −3 thread/Makefile
  2. +3 −3 thread/test/ExitDeadLock.cc
  3. +86 −0 thread/test/MutualDeadLock.cc
  4. +25 −0 thread/test/SelfDeadLock.cc
View
12 thread/Makefile
@@ -12,7 +12,10 @@ TESTS= atomic_unittest \
threadlocal_test \
threadlocalsingleton_test \
thread_test \
- threadpool_test
+ threadpool_test \
+ exit_deadlock \
+ mutual_deadlock \
+ self_deadlock
all: $(TESTS)
@@ -27,8 +30,11 @@ blockingqueue_test: test/BlockingQueue_test.cc Thread.cc
boundedblockingqueue_test: test/BoundedBlockingQueue_test.cc Thread.cc
-exit_deadlock: test/ExitDeadLock.cc
- g++ $(CXXFLAGS) -o $@ $(filter %.cc,$^) -lpthread
+exit_deadlock: test/ExitDeadLock.cc Thread.cc
+
+mutual_deadlock: test/MutualDeadLock.cc Thread.cc
+
+self_deadlock: test/SelfDeadLock.cc Thread.cc
exception_test: test/Exception_test.cc Exception.cc
View
6 thread/test/ExitDeadLock.cc
@@ -5,9 +5,9 @@
using namespace muduo;
-void someFunctionCallsExit()
+void someFunctionMayCallExit()
{
- exit(1);
+ exit(1);
}
class GlobalObject
@@ -16,7 +16,7 @@ class GlobalObject
void doit()
{
MutexLockGuard lock(mutex_);
- someFunctionCallsExit();
+ someFunctionMayCallExit();
}
~GlobalObject()
View
86 thread/test/MutualDeadLock.cc
@@ -0,0 +1,86 @@
+#include "../Mutex.h"
+#include "../Thread.h"
+#include <set>
+#include <stdio.h>
+
+class Request;
+
+class Inventory
+{
+ public:
+ void add(Request* req)
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ requests_.insert(req);
+ }
+
+ void remove(Request* req) __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ requests_.erase(req);
+ }
+
+ void printAll() const;
+
+ private:
+ mutable muduo::MutexLock mutex_;
+ std::set<Request*> requests_;
+};
+
+Inventory g_inventory;
+
+class Request
+{
+ public:
+ void process() // __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ g_inventory.add(this);
+ // ...
+ }
+
+ ~Request() __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ sleep(1);
+ g_inventory.remove(this);
+ }
+
+ void print() const __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ // ...
+ }
+
+ private:
+ mutable muduo::MutexLock mutex_;
+};
+
+void Inventory::printAll() const
+{
+ muduo::MutexLockGuard lock(mutex_);
+ sleep(1);
+ for (std::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()
+{
+ muduo::Thread thread(threadFunc);
+ thread.start();
+ usleep(500);
+ g_inventory.printAll();
+ thread.join();
+}
View
25 thread/test/SelfDeadLock.cc
@@ -0,0 +1,25 @@
+#include "../Mutex.h"
+
+class Request
+{
+ public:
+ void process() // __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ print();
+ }
+
+ void print() const // __attribute__ ((noinline))
+ {
+ muduo::MutexLockGuard lock(mutex_);
+ }
+
+ private:
+ mutable muduo::MutexLock mutex_;
+};
+
+int main()
+{
+ Request req;
+ req.process();
+}

0 comments on commit d3811de

Please sign in to comment.
Something went wrong with that request. Please try again.