The condition of Android analytics

Source: Internet
Author: User

The meaning of condition is the conditional variable, its implementation depends on the system, generally with the mutex to use, the use of the steps are: to lock the mutex, call wait Wait "condition" occurs, if not occurred then re-wait (), Finally releases the mutex (unlock) and continues execution. All threads that wait for the same condition variable (condition) will use the same lock-this is equivalent to the mutex operation of the condition.

---------------------------------------------------------------------------namespace Android {//--------------  -------------------------------------------------------------/* * Condition variable class. The implementation is system-dependent.  * * Condition variables is paired up with mutexes. Lock the Mutex, * call Wait (), then either re-wait () if things aren ' t quite what do you want, * or unlock the mutex and Conti  Nue. All threads calling Wait () must * with the same mutex for a given Condition.    */class Condition {public:enum {PRIVATE = 0,//Specifies that the free cross-process share shared = 1};    Condition ();    Condition (int type);    ~condition ();  Wait on the condition variable.    Lock the mutex before calling. status_t Wait (mutex& mutex);//Mutex access to "condition variable" via mutex-multithreaded, multi-process scenario//Same with relative timeout status_t Waitrelati    ve (mutex& Mutex, nsecs_t reltime);    Signal the condition variable, allowing one thread to continue.    void signal (); Signal the Condition variable, allowing all threads to continue. void broadcast ();p rivate: pthread_cond_t type for #if defined (have_pthreads) mcond;//linux pthread_cond_t #else void* mStat e; #endif};//---------------------------------------------------------------------------#if defined (have_ PTHREADS) 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) {//System call wait contains: Release lock, go to hibernation wait, regain lock after wake-up return-pthread_cond_wait (& Mcond, &mutex.mmutex);} Inline status_t condition::waitrelative (mutex& Mutex, nsecs_t Reltime) {//Has time control # if defined (have_pthread_cond_timedwait_relative) struct TIMESPEC ts;    Ts.tv_sec = reltime/1000000000;    Ts.tv_nsec = reltime%1000000000; RETURN-PTHREAD_COND_TIMEDWAIT_RELATIVE_NP (&mcond, &mutex.mmutex, &ts); #else//HAVE_PTHREAD_COND_  timedwait_relative struct TIMESPEC ts; #if defined (have_posix_clocks) clock_gettime (Clock_realtime, &ts); #else//    Have_posix_clocks//We don ' t support the clocks.    struct Timeval t;    Gettimeofday (&t, NULL);    Ts.tv_sec = t.tv_sec;    ts.tv_nsec= t.tv_usec*1000; #endif//have_posix_clocks ts.tv_sec + = reltime/1000000000;    ts.tv_nsec+= reltime%1000000000;        if (ts.tv_nsec >= 1000000000) {ts.tv_nsec-= 1000000000;    Ts.tv_sec + = 1; } return-pthread_cond_timedwait (&mcond, &mutex.mmutex, &ts); #endif//Have_pthread_cond_timedwait_ relative}inline void Condition::signal () {pthread_cond_signal (&mcond);} inline void Condition::broadcast () {PthrEad_cond_broadcast (&mcond);} #endif//have_pthreads//---------------------------------------------------------------------------}; Namespace Android

Barrier is an example of using the above condition:

Class Barrier{public:    inline Barrier (): state (CLOSED) {}//state is called "condition"    inline ~barrier () {}    void Open () {        Mutex::autolock _l (lock);        state = opened;        Cv.broadcast ();    }    void Close () {        mutex::autolock _l (lock);        state = CLOSED;    }    void Wait () const {        mutex::autolock _l (lock),///temporary object _l, constructed with lock, given lock locking (call Lock () function) in Autolock constructor--The Wait () Once the function is executed, lock is automatically released (this scenario causes other threads to modify state again, creating an unsafe factor.) However, due to the particularity of the use scene of barrier, when using the thread initialization, it is OK. While        (state = = CLOSED) {//while statement: Polling continuously until state==opened            cv.wait (lock);        }    } Private:    enum {opened, CLOSED};    mutable     Mutex       lock;//holds a mutex    mutable     Condition   cv;//holds a condition variable    volatile    int         state;//updates the "condition"} from memory every time;

  

The condition of 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.