Release a C ++ multi-thread library for Linux written in my spare time. This library contains less than 1000 lines.Source codeIt encapsulates common pthreads functions (mutex, condition variable, thread), implements a simple thread pool, and compiles blockingqueue and countdownlatch in accordance with the Java concurrent package. Every class in the Library provides an example.
The content of this library:
- Atomic integer operations: atomicint32 and atomicint64
- Thread
- Thread Pool, threadpool
- Mutexlock, mutexlockguard, and condition
- Exception base class with stack trace information
- Similar to Java concurrent blockingqueue and countdownlatch
- Singleton and threadlocal
Note: I did not provide the semaphore encapsulation. In the future, the read/write lock encapsulation may be added, if I use it in my blog.
The interfaces of thread and threadpool adopt the style advocated in "replace virtual functions with boost: function and boost: bind". Inheritance and base classes are not used.
Note that currentthread has a thread local variable t_threadname, which is used to print the thread name during debugging and Analysis of core dump. For example:
(GDB) p'muduo: currentthread: t_threadname'
$4 = 0x4057fe "threadpool2"
For the use of mutexlock, mutexlockguard, and condition, see common programming models of multi-threaded servers.
For the example of countdownlatch, see test/blockingqueue_test.cc.
Git: http://github.com/chenshuo/recipes
Browsing SourceCode: Http://github.com/chenshuo/recipes/tree/master/thread/
This library has been compiled and tested under Debian squeeze and Ubuntu 10.04 lts for x86 and x86-64 platforms.