C + + Encapsulation mutex object

Source: Internet
Author: User

In multithreaded programs, in order to prevent the concurrency of threads, it is often necessary to use the mutex for data protection. POSIX provides phtread_mutex_t for mutually exclusive protection data. The use of mutexes requires initialization and release correspondence (Phtread_mutex_init () and Phtread_mutex_destroy () correspondence), locking and unlocking correspondence (Phtread_mutex_lock and Pthread_mutex_ Unlock corresponding). The process of lock and unlock is part of the design logic. Generally, the programmer can correctly lock and unlock the corresponding, but to prevent the program after lock or return without unlock. Not releasing after initializing a mutex can also cause a resource leak, which is also a very easy place to miss. In the actual development of the general need to encapsulate their own mutex.

Class Mutexlock {Public:mutexlock (): Holder_ (0) {int ret = Pthread_mutex_init (&mutex_, NULL);   ASSERT (ret = = 0);    } ~mutexlock () {assert (Holder_ = = 0);    int ret = Pthread_mutex_destroy (&mutex_);   ASSERT (ret = = 0);  } bool Islockedbythisthread () const {return holder_ = = static_cast<pid_t> (:: Syscall (Sys_gettid));  } void assertlocked () const {ASSERT (Islockedbythisthread ());    } void Lock () {pthread_mutex_lock (&mutex_);  Assignholder ();    } void Unlock () {Unassignholder ();  Pthread_mutex_unlock (&AMP;MUTEX_);  } pthread_mutex_t* Getpthreadmutex () {return &mutex_;  } private:mutexlock (const Mutexlock &);  Mutexlock &operator= (const Mutexlock &);  void Unassignholder () {holder_ = 0;  } void Assignholder () {holder_ = static_cast<pid_t> (:: Syscall (Sys_gettid));  } pthread_mutex_t mutex_; pid_t Holder_;}; Class Mutexlockguard {public:explicit Mutexlockguard (mutexlock& Mutex): Mutex_ (Mutex) {mutex_.lock ();  } ~mutexlockguard () {mutex_.unlock ();  } private:mutexlockguard (const Mutexlockguard &);  Mutexlockguard &operator= (const Mutexlockguard &); mutexlock& mutex_;};

In order to improve the usability of Mutextlock, a Mutexlockguard class is added to encapsulate Mutextlock, which is used directly when using Mutexlockguard, This prevents forgetting to release the mutex (Mutexlockguard out-of-scope (typically a stack variable) and automatically releases it, calls the destructor, destroy the mutex).

Note: In fact, in the C++11 thread library already have lock guard can be used directly (Std::lock_guard, only need include<mutex>), do not need to write again, for not migrated to C + + Projects on 11 can use their own encapsulated mutex to improve usability.

C + + Encapsulation mutex object

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.