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.