C + + multithreaded Framework (2): Mutex mutex and Sem semaphore

Source: Internet
Author: User
Tags semaphore
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



Initialize Mutex lock



Lock operation



Unlock action



For different systems just implement the functions are some different, but the function is actually very much the same, in the lock operation and unlock operation when most of the system has a timeout mechanism in the inside, to ensure that not always locked in a place, we for the framework is simple, there is no set timeout, when the lock operation if not lock, Would have been waiting there.



The base class of the mutex we describe as follows



class CMutex
{
     public:
         CMutex (const char * pName = NULL); // Initialize 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, 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 SEM, the implementation of each system is similar, nothing more than



Initialize the semaphore



Send semaphore (signal volume + 1)



Received signal volume (semaphore-1)






The SEM base class is described below



class CCountingSem
{
     public:
         typedef enum {
                  kTimeout,
              kForever
             } Mode;
         CCountingSem (); // Initialize the semaphore
         ~ CCountingSem ();
          virtual bool Get () = 0; // receive semaphore
              virtual bool Post (void) = 0; // Send semaphore
  };





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 * newCountingSem (unsigned int init = 0); // The parameter is the initial value of the semaphore, generally 0
         static CMutex * newMutex (const char * 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



The above is the C + + multithreaded Framework (2): Mutex mutex and Sem semaphore content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

Related Article

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.