C ++ multithreading framework (ii) --------- Mutex and Sem semaphore

Source: Internet
Author: User

Mutex and semaphores are two foundations of multi-thread programming. The principle of mutex and semaphores is not detailed. Let's look at the operating system books or check it online.
For mutex implementation, no matter What Operating System is inseparable from three steps
1. initialize the mutex lock
2. Lock Operation
3. Unlock


Different systems only implement different functions, but the functions are similar. Most systems have timeout mechanisms during lock and unlock operations, to ensure that the lock is not always somewhere. For the sake of simplicity of the framework, no timeout is set. If the lock is not obtained during the lock operation, it will remain there.


The base class of Mutex is described as follows:
[Cpp] class CMutex
{
Public:
CMutex (const char * pName = NULL); // initialization lock
~ CMutex ();
Virtual bool Lock () = 0; // Lock operation, pure virtual function
Virtual bool UnLock () = 0; // UnLock operation, pure virtual function
Const char * getName (void) const {
Return mutex_name;
}
Protected:
Char * mutex_name; // lock name
};

Class CMutex
{
Public:
CMutex (const char * pName = NULL); // initialization lock
~ CMutex ();
Virtual bool Lock () = 0; // Lock operation, pure virtual function
Virtual bool UnLock () = 0; // UnLock operation, pure virtual function
Const char * getName (void) const {
Return mutex_name;
}
Protected:
Char * mutex_name; // lock name
};

For each system implementation, initialization, lock operation, and unlock operation must be completed. In linux, these three operations are simple and will not be pasted with code here.


Similarly, for semaphores Sem, the implementation of each system is similar, nothing more
1. initialize semaphores
2. Sending semaphores (semaphores + 1)
3. Receive semaphores (semaphores-1)


The Sem base class is described as follows:
[Cpp] class CCountingSem
{
Public:
Typedef enum {
KTimeout,
KForever
} Mode;
CCountingSem (); // initialize the semaphore
~ CCountingSem ();
Virtual bool Get () = 0; // receives semaphores
Virtual bool Post (void) = 0; // sends a semaphore
};

Class CCountingSem
{
Public:
Typedef enum {
KTimeout,
KForever
} Mode;
CCountingSem (); // initialize the semaphore
~ CCountingSem ();
Virtual bool Get () = 0; // receives semaphores
Virtual bool Post (void) = 0; // sends a semaphore
};

Similarly, the specific implementation will not post code.
Of course, for a system that meets the design pattern, when creating mutex locks and semaphores, of course, these classes cannot be new directly, and must be returned through a simple project, add the newMutex and newCountingSem methods to the COperatingSystemFactory class and return the corresponding entities through COperatingSystemFactory's judgment on the operating system.
[Cpp] class COperatingSystemFactory
{
Public:
Static COperatingSystem * newOperatingSystem ();
Static CCountingSem * newCountingSem (unsigned int init = 0); // The parameter is the initial value of the semaphore, usually 0
Static CMutex * newMutex (const char * pName = NULL );
};

Class COperatingSystemFactory
{
Public:
Static COperatingSystem * newOperatingSystem ();
Static CCountingSem * newCountingSem (unsigned int init = 0); // The parameter is the initial value of the semaphore, usually 0
Static CMutex * newMutex (const char * pName = NULL );
};

 


Well, with mutex and semaphores, how can we use them? In the main function, we can apply for mutex locks and semaphores first. If we enable multithreading, if the mutex lock is required between a few, we assign the applied mutex lock to the corresponding thread to use it directly. As for the various thread classes, you write them by yourself. They only inherit from CThread. How do the member variables in them relate to the mutex lock applied in main, you can also set it to public value assignment. You can also set it from the private function set. Everything depends on you.


With mutex lock and semaphores, you can start a message queue. With message queue, a simple multi-threaded model is complete.


 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.