Implementation of mutex and condtion for Thread Synchronization in Android

Source: Internet
Author: User
Tags pthread mutex
Tian haili

Android-encapsulated synchronization classes mainly include mutex (automutex) and condition. The previous article analyzes their external use. This article describes how they are implemented through encapsulation of pthread mutex and condition. The usage of pthread can be viewed in conjunction with the usage of mutex and condition of pthread.
Keywords: mutex, condition variable, autolock/automutex, Android
Keywords: mutex, condition variable, automatic lock/automatic mutex, Android

1. Implementation of mutex

The implementation of mutex is in frameworks/native/include/utils/mutex. h.

The implementation of mutex utilizes the implementation of pthread mutex.

First, class mutex internally defines a pthread_mutex

    pthread_mutex_t mMutex;

Then, pthread_mutex_xxx () is encapsulated respectively:

inline Mutex::Mutex() {    pthread_mutex_init(&mMutex, NULL);}inline Mutex::Mutex(const char* name) {    pthread_mutex_init(&mMutex, NULL);}inline Mutex::Mutex(int type, const char* name) {    if (type == SHARED) {        pthread_mutexattr_t attr;        pthread_mutexattr_init(&attr);        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);        pthread_mutex_init(&mMutex, &attr);        pthread_mutexattr_destroy(&attr);    } else {        pthread_mutex_init(&mMutex, NULL);    }}inline Mutex::~Mutex() {    pthread_mutex_destroy(&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);}

For more information about pthread mutex usage, see pthread mutex and condition usage.

II. Implementation of autolock/automutex

The implementation of autolock/automutex is also in frameworks/native/include/utils/mutex. h.

namespace android {class Mutex {    //...    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;    };}; // end of class Mutextypedef Mutex::Autolock AutoMutex;}; // namespace android

Iii. Implementation of Condition

Condition is implemented in frameworks/native/include/utils/condition. h.

The implementation of condition utilizes the implementation of pthread condition.

First, class condition defines a pthread_cond

    pthread_cond_t mCond;

Then, pthread_cond_xxx () is encapsulated respectively:

inline Condition::Condition() {    pthread_cond_init(&mCond, NULL);}inline Condition::Condition(int type) {    if (type == SHARED) {        pthread_condattr_t attr;        pthread_condattr_init(&attr);        pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);        pthread_cond_init(&mCond, &attr);        pthread_condattr_destroy(&attr);    } else {        pthread_cond_init(&mCond, NULL);    }}inline Condition::~Condition() {    pthread_cond_destroy(&mCond);}inline status_t Condition::wait(Mutex& mutex) {    return -pthread_cond_wait(&mCond, &mutex.mMutex);}inline void Condition::signal() {    pthread_cond_signal(&mCond);}inline void Condition::broadcast() {    pthread_cond_broadcast(&mCond);}

For more information about pthread condition usage, see pthread mutex and condition usage.

Iv. Summary

This article briefly introduces the internal implementation of the commonly used synchronization mechanism mutex (automutex) and condition in Android, that is, the simple encapsulation of pthread mutex and condition. The usage of mutex and condition in pthread is described in usage of mutex and condition in pthread.

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: 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.