In this article we will discuss the encapsulation of threads thread, mutex mutexlock, condition variable condition;
Description
1, Mutexlock, Condition Use the constructor function and the destructor to complete the application and release of the resources automatically ;
2, Mutexlock, condition, and thread are all related to system resources, these classes are all non-replicable ; for example, a program initiates a lock, and another program B copies the lock, and if a program destroys the lock at some point, at any rate, b programs cannot have locks on this system resource. Therefore, we use it as a private inheritance of the noncopyable class .
First, Noncopyable class:
1 #ifndef Noncopyable_h_2 #defineNpncopyable_h_3 4 classNoncopyable//Disable value semantics5 {6 Public:7 noncopyable () {}8~noncopyable () {}9 Ten Private: OneNoncopyable (ConstNoncopyable &);//no copy A void operator=(ConstNoncopyable &);//No Assignment - }; - the #endif
Second, the Mutex lock Class (Mutexlock), the code is as follows:
1 #ifndef Mutex_lock_h2 #defineMutex_lock_h3#include"NonCopyable.h"4#include"pthread.h"5#include <assert.h>6 7 //Attention8 classmutexlock:noncopyable9 {Ten Public: One Mutexlock (); A~Mutexlock (); - - void Lock();//locked the voidUnlock ();//Release Lock - BOOLIslocking ()Const{returnIslocked_;}//determine the state of a lock - pthread_mutex_t Getmutex () -{returnmutex_;} + Private: - pthread_mutex_t mutex_; + BOOLIslocked_;//flag is locked A }; at - Mutexlock::mutexlock () -: Islocked_ (false) - { -Pthread_mutex_init (&mutex_, NULL); - } inmutexlock::~Mutexlock () - { to //determine if the lock has been unlocked +ASSERT (!islocking ()); -Pthread_mutex_destroy (&mutex_); the } * voidMutexlock::Lock() $ {Panax NotoginsengIslocked_ =true; -Pthread_mutex_lock (&mutex_); the } + voidMutexlock::unlock () A { theIslocked_ =false; +Pthread_mutex_unlock (&mutex_); - } $ $ #endif
Three, the Condition variable class (Condition):
1 #ifndef Condition_h_2 #defineCondition_h_3 4#include"NonCopyable.h"5#include"MutexLock.h"6#include <pthread.h>7 8 classCondition:noncopyable//Private Inheritance9 {Ten Public: OneCondition (Mutexlock &mutex); A~Condition (); - - voidWait ();//wait the voidSignal ();//Send Signal - Private: - pthread_cond_t Cond_; -Mutexlock &mutex_;//parameters for the wait function + }; - +Condition::condition (Mutexlock &mutex) A : Mutex_ (Mutex) at { -Pthread_cond_init (&Cond_, NULL); - } - -condition::~Condition () - { inPthread_cond_destroy (&cond_); - } to + voidcondition::wait () - { thepthread_mutex_tLock=Mutex_.getmutex (); *Pthread_cond_wait (&cond_, &Lock); $ }Panax Notoginseng - voidcondition::signal () the { +Pthread_cond_signal (&cond_); A } the #endif
Threading Class (thread):
Note :
1, the thread by default is joinable(can be combined state), you need to call the Join function manually (recycle it), you can also set it to detachable( detach State ), the thread dies automatically after running;
2, the thread class uses the static function as the callback function of the pthread_create, because the ordinary member function contains an implicit parameter (the class's object itself), so the function pointer type In addition to the void* parameter, there is a the implicit parameter of the Thread object, so this does not match the void* (*) (void*) parameter;
the declaration of a class member is as follows :
1 Private : 2 void *runinthread (void// Here is the ordinary member function 3 pthread_t tid_; 4 bool isruning_;
Pthread_create:
1 void*thread::runinthread (void*Arg)2 {3cout <<"Foo"<<Endl;4 }5 6 voidThread::start ()7 {8 //the Runinthread parameter here is two, one is an object of thread (implied parameter) and the other is void*9 //WORKAROUND: Declare runinthread as a static functionTenPthread_create (&tid_, NULL, thread::runinthread,null);//wrong OneIsruning_ =true; A}
The encapsulation of the Thread of Linux Learning