Mutual exclusion and semaphore is the two basis of multi-threaded programming, its principle is not detailed said, we go to see the operating system of the book or online check it. For mutually exclusive implementations, no matter what operating system is inseparable from the three steps 1. To initialize a mutex 2. Lock Operation 3. The unlocking operation is somewhat different for the functions implemented by different systems, but the functions are quite similar, and most systems have a timeout mechanism in the lock operation and unlocking operation. To ensure that it will not always be locked in a place, we have a simple frame, no set timeout, lock operation when the lock, will be waiting there. The base class of the mutex we describe as follows
classCMutex { Public: CMutex (Const Char*pname = NULL);//Initialize lock~CMutex (); Virtual BOOLLock () =0;//lock operation, pure virtual function Virtual BOOLUnLock () =0;//unlock operation, pure virtual function Const Char* GetName (void)Const { returnMutex_name; } protected: Char*mutex_name;//Lock Name};
For each system implementation, all need to complete initialization, lock operation, unlock operation three parts, under Linux, these three operations are very simple, it is not here to paste code. Similarly, for the signal volume of SEM, the implementation of each system is similar, nothing more than 1. Initialize the Semaphore 2. Signal Volume (semaphore + 1) 3. Received signal volume (semaphore-1) The SEM base class is described as follows
classCcountingsem { Public: typedefenum{ktimeout, kforever}mode; Ccountingsem (); //Initialize the semaphore~Ccountingsem (); Virtual BOOLGet () =0;//received signal volume Virtual BOOLPost (void) =0;//Send Signal Volume};
Similarly, the implementation is not affixed to the code. Of course, for a system that satisfies the design pattern, it is certainly not possible to create new mutexes and semaphores directly, and inevitably to return through simple engineering, adding Newmutex and Newcountingsem methods to the Coperatingsystemfactory class. , the corresponding entity is returned by coperatingsystemfactory the judgment of the operating system.
class coperatingsystemfactory { public: static Coperatingsystem *newoperatingsystem (); Static Ccountingsem int init=0// parameter is the initial value of the semaphore, typically 0 Static CMutex *newmutex (constchar *pname=NULL); };
Well, with mutual exclusion and semaphore, how to use it, in the main function, we can first apply for a mutex and semaphore, if we start a lot of threads, if we need a mutual exclusion between a few, then we will apply a good mutex to the corresponding thread, you can directly use. As for each thread class, you write your own, just inherit from Cthread, the inside of the member variable and main in the application of the mutex association, it is needless to say, you set it to public assignment is also OK, set from the private function set also line, everything to see you. With the mutex and semaphore, the message queue is now available, and with Message Queuing, one of the simplest multithreaded models is completed. GitHub Address: Https://github.com/wyh267/Cplusplus_Thread_Lib reference: http://blog.csdn.net/ygrx/article/details/8963784
C + + multithreaded framework-----Mutex mutex and SEM semaphore