Since the functions of the Pthread series are all successful and return 0, we need a code to judge the processing error:
#define Tiny_check (exp) if(! EXP) { "File:%s, line:%d Exp: ["#exp"] is true, abort.\n " , __file__, __line__); Abort (); }
The Mutexlock package is as follows:
1 #ifndef Mutexlock_h2 #defineMutexlock_h3 4#include"NonCopyable.h"5#include <pthread.h>6 #defineTiny_check (exp)7 if(!exp)8 { 9fprintf (stderr,"File:%s, line:%d Exp: ["#exp"] is true, abort.\n", __file__, __line__); Abort ();Ten } One A classmutexlock:noncopyable - { - Public: the Mutexlock (); -~Mutexlock (); - void Lock(); - voidunlock (); + - BOOLIsLocked ()Const{return_islock;} +pthread_mutex_t *getmutexptr () {return&_mutex;} A Private: at pthread_mutex_t _mutex; - BOOL_islock; - }; - - #endif
CPP files are as follows:
1#include"MutexLock.h"2#include <assert.h>3 4 Mutexlock::mutexlock ()5: _islock (false)6 {7Tiny_check (!pthread_mutex_init (&_mutex, NULL));8 }9 Tenmutexlock::~Mutexlock () One { AASSERT (!isLocked ()); -Tiny_check (!pthread_mutex_destroy (&_mutex)); - } the - voidMutexlock::Lock() - { -Tiny_check (!pthread_mutex_lock (&_mutex)); +_islock =true; - } + A voidMutexlock::unlock () at { -_islock =false; -Tiny_check (!pthread_mutex_unlock (&_mutex)); -}
Linux component Encapsulation (i) Mutex lock Mutexlock