Android C + + thread synchronization implementation

Source: Internet
Author: User
Tags mutex

Preface

Thread synchronization in Android C + + is primarily a encapsulation of pthread mutexes and condition. So before learning, it is recommended to understand the implementation of a standard C + + thread synchronization, reference link: C + + thread learning

Mutex

Android Mutex Implementation source code is located in/system/core/include/utils/mutex.h, let's take a look at the specific implementation of the mutex class:

Class Mutex { Public:enum{PRIVATE =0, SHARED =1};    Mutex (); Mutex (Const Char* name); Mutex (intTypeConst Char* name = NULL); ~mutex ();//Lock or unlock the mutexstatus_tLock();voidUnlock ();//Lock if possible; Returns 0 on success, error otherwisestatus_t Trylock (); Class Autolock { Public: inlineAutolock(mutex& Mutex):MLock(mutex) {MLock.Lock(); } inline Autolock (mutex* Mutex): MLock (*mutex) {MLock.Lock(); } inline ~autolock () {Mlock.unlock ();}Private: mutex& MLock; };Private: pthread_mutex_t Mmutex;};

From the scratch file, we found that the mutex class private variable Mmutex is our familiar phread_mutex_t type, Pthread_mutex_lock and pthread_mutex_ The unlock function guarantees the atomicity of a series of operations through this variable.

Next, let's take a look at the implementation of the mutex class constructors, destructors, lock, Unlock, and trylock (before we look at the source code, we should be able to guess that you are the simple encapsulation of the pthread _mutex_xxx function):

inline Mutex::Mutex() {    // 构造函数,初始化mMutex变量    NULL);}inline Mutex::~Mutex() {    // 析构函数,就是销毁mMutex变量    pthread_mutex_destory(&mMutex);}inline status_t Mutex::lock() {    return -pthread_mutex_lock(&mMutex);}inlinevoid Mutex::unlock() {    pthread_mutex_unlock(&mMutex);}inline status_t Mutex::tryLock() {    return pthread_mutex_tryLock(&mMutex);}

Just this simple package is not enough to satisfy the lazy mind of Android designers. And we see in a lot of Android source code similar to:

AutoMutex _l(gProcessMutex);

Such a notation does not see the Mutex::lock () and Mutex::unlock () of the display call. So, next, we continue to learn Automutex based on the implementation of the mutex class.

Automutex

Automutex is only a typedef rename, the source code is as follows:

typedefMutex::AutolockAutoMutex;

The Mutex::autolock is the inner class of the mutex class, realize the source code as follows:

class  autolock  {public:inline autolock   (mutex  & mutex ) : Mlock (mutex )  {Mlock.lock () ; } inline autolock   (mutex  * mutex ) : Mlock (* mutex )  {Mlock.lock () ;} Inline ~autolock   ()  {Mlock.unlock () ; }private: mutex  & MLock;};  

As you can see, the Autolock inner class invokes the lock method of the mutex class in the constructor, and the unlock method of the mutex class is called in the destructor. So when we call Automutex in a function block:

Mutex gProcessMutex;void testAutoMutex() {    AutoMutex _l(gProcessMutex);    // 下面是需要同步的代码内容    ...}

We define a Automutex variable _l, and assign the variable Gprocessmutex of the mutex type to _l to initialize the constructor, and naturally implement the lock method called Gprocessmutex, which realizes the thread lock mechanism.
When the function is finished, Automutex's destructor is called, and the Gprocessmutex unlock method is called, releasing the thread lock mechanism.

In fact, the mechanism of Automutex is a bit similar to the smart pointer, it is very good to use the constructor and destructor of the class to do some specific operations.

Android C + + thread synchronization implementation

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.