Synchronous Implementation of threads in Android C ++ and android threads

Source: Internet
Author: User

Synchronous Implementation of threads in Android C ++ and android threads
Preface

In Android C ++, thread synchronization mainly encapsulates the mutex and condition of pthread. So before learning, we recommend that you first understand the implementation of Thread Synchronization in Standard C ++. For more information, see the link: thread learning in C ++.

Mutex

The implementation source code of Android Mutex is located at/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(int type, const char* name = NULL);    ~Mutex();    // lock or unlock the mutex    status_t lock();    void unlock();    // lock if possible; returns 0 on success, error otherwise    status_t tryLock();    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;    };private:    pthread_mutex_t mMutex;};

From the original file, we found that the Mutex private variable mMutex is a familiar phread_mutex_t type. The pthread_mutex_lock and pthread_mutex_unlock functions use this variable to ensure the atomicity of a series of operations.

Next, let's take a look at the implementation of Mutex class constructor, destructor, lock, unlock, and tryLock (before looking at the source code, we should be able to guess that you are a simple encapsulation of the pthread _ mutex_xxx function ):

Inline Mutex: Mutex () {// constructor, initialize the mMutex variable pthread_mutex_init (& mMutex, NULL);} inline Mutex ::~ Mutex () {// destructor, that is, destroy the mMutex variable pthread_mutex_destory (& mMutex) ;}inline status_t Mutex: lock () {return-pthread_mutex_lock (& mMutex );} inline void Mutex: unlock () {pthread_mutex_unlock (& mMutex);} inline status_t Mutex: tryLock () {return pthread_mutex_tryLock (& mMutex );}

This is just a simple encapsulation, and it cannot satisfy the psychology of Android designers being lazy. In addition, we can see a lot in the Android source code that is similar:

AutoMutex _l(gProcessMutex);

In this way, the display call of Mutex: lock () and Mutex: unlock () is not displayed. Therefore, we will continue to learn how to implement AutoMutex Based on the Mutex class.

AutoMutex

AutoMutex is just a Rename of typedef. The source code is as follows:

typedef Mutex::Autolock AutoMutex;

Mutex: Autolock is an internal class of the Mutex class. The source code is 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 internal class calls the lock method of the Mutex class in the constructor, and calls the unlock method of the Mutex class in the destructor. So when we call AutoMutex in a function block:

Mutex gProcessMutex; void testAutoMutex () {AutoMutex _ l (gProcessMutex); // the following code content to be synchronized //...}

We have defined an AutoMutex Variable _ l, and assigned the gProcessMutex variable of the Mutex type to _ l for initialization of the constructor. Naturally, the lock method of gProcessMutex is called, implements the thread lock mechanism.
After the function is executed, the AutoMutex destructor is called to call the unlock method of gProcessMutex and release the thread lock mechanism.

In fact, the AutoMutex mechanism is a bit similar to smart pointers. They all use class constructor and destructor to perform some specific operations.

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.