The mutex for Android analytics

Source: Internet
Author: User

The Android Lock is a wrapper for the Linux lock:

---------------------------------------------------------------------------namespace Android {//--------------  -------------------------------------------------------------class condition;/* * Simple Mutex class. The implementation is system-dependent.  * The mutex must is unlocked by the thread that locked it. They is not * recursive, i.e. the same thread can ' t lock it multiple times.                    */class Mutex {public:enum {PRIVATE = 0,//This lock is used within this process using SHARED = 1//The lock is used across processes};                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 (); Manages the mutex automatically.    It ' ll be locked when Autolock was//constructed and released when Autolock goes out of scope. Class Autolock {//internal class, used to manage mutexes--equivalent to "smart pointer" publIc:inline Autolock (mutex& Mutex): MLock (Mutex) {Mlock.lock ();}        Inline Autolock (mutex* Mutex): MLock (*mutex) {Mlock.lock ();}    Inline ~autolock () {Mlock.unlock ();}        private:mutex& mlock;//holds a reference to a mutex};p Rivate:friend class condition;//friend, Condition is used on the basis of a mutex A mutex cannot be copied mutex (const mutex&);//set to private, unable to invoke "copy constructor" mutex& operator = (con    St mutex&);    #if defined (have_pthreads) pthread_mutex_t mmutex;//uses the Linux pthread_mutex_t this mutex #else void _init (); void* mstate, #endif};//---------------------------------------------------------------------------#if defined ( have_pthreads) 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) {//if branch: Determines whether to construct a "cross-process" lock based on the value passed in Pthread_mute        xattr_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);//if branch: Lock}}inline in this process 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);} #endif//have_pthreads//---------------------------------------------------------------------------/* * Automatic  Mutex. Declare one of these at the top of a function. * When the function returns, it'll go out of scope, and release the * mutex. */typedef mutex::autolock AUTOMUTEX;//-------------------------------------------------------------------------- -};   Namespace Android

  

The mutex for Android analytics

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.