In the startthreadpool () function of frameworks/base/libs/binder/processstate. cpp,
The function was declared at the beginning:
Automutex _ L (mlock );
It's not complicated, but I feel very thoughtful.
In frameworks/base/include/utils/Threads. h, there are:
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 ;};......}; typedef mutex: autolock automutex;
We can see that mlock. Lock () is locked in the constructor and mlock. Unlock () is unlocked in the destructor.
Therefore, for a function to be locked, we only need to declare the automutex _ L (mlock) variable at the beginning of the function, and it will lock,
When the function exits, a temporary variable will be parsed and unlocked.
Really clever !!!
Object-oriented is really a subtle learning.
I remembered that I had defined an object that generates a random number and used seed in the constructor to prevent the result of selecting seed for every function call.
Because the latter has the same seed every time, the re-generated random number is the same.