android底层库libutils之Condition研究

博文是自己学习总结所得,如有错误,欢迎大家指正,大家一起学习,共同进步!

1)contidion认识

线程1做初始化工作,而其它线程例如线程2,线程3,必须等待初始化工作完成之后才能进行后续的操作,那么此时线程2,线程3在等待同一个条件。当线程1完成初始化工作之后,它会触发这个条件,那么线程2,线程3就有机会被唤醒。如果有多个线程在等这个条件的话,那么只能唤醒一个线程。由类实现可以看到condiion仍然是封装的Linux标准api,我们没什么可以说的。

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) { //当线程调用wait后,线程就开始等待条件
    return -pthread_cond_wait(&mCond, &mutex.mMutex);
}
inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime){ //超时等待,当超过了这个时间,就退出等待。
........
}
inline void Condition::signal() { //调用此函数,通知其它线程条件满足,可以在实例中看到
    pthread_cond_signal(&mCond);
}
inline void Condition::broadcast() {
    pthread_cond_broadcast(&mCond);
}

Condition在使用的过程中一般结合一个Mutex一起使用。当我们调用conditon::wait()方法时,其实执行的是pthread_cond_wait,该函数在一个conditon上阻塞等待,其中要做3个步骤:1.释放锁Mutex、2.阻塞等待(条件不成立)、3.当其它线程调用signal或broadcast时,它会重写获取锁。

2)Condition实例

实例源码可以在链接中下载.

Condition con;
Mutex mutex;
int p = 0;
void *thread1(void *ptr)
{
    mutex.lock(); 
    con.wait(mutex); //线程1拿到锁之后,在这里会释放,等条件满足后会再次拿到锁。
    mutex.unlock();

    cout << "thread1 start!." << endl;
    while(1){
        sleep(1);
        cout << "This is thread1." << endl;
    }
    return 0;
}

void *thread2(void *ptr)
{
    int i=0;
    cout << "thread2 start!." << endl;
    while(1){
        sleep(1);
        i++;
        cout << "This is thread2." << endl;
        if(i == 5)
            con.signal();  //等线程2打印5次log后,才会唤醒线程1.
    }
    return 0;
}

int main() {
    pthread_t id1;
    pthread_t id2;
    int ret = pthread_create(&id1, NULL, thread1, NULL);
    if(ret) {
        cout << "Create pthread1 error!" << endl;
        return 1;
    }
    ret = pthread_create(&id2, NULL, thread2, NULL);
    if(ret) {
        cout << "Create pthread2 error!" << endl;
        return 1;
   } 
    while(1){
        cout <<  "This is the main process." << endl;
        sleep(1);
    }
    return 0;
}
下面是运行时打印的log,可以发现我们一开始先创建了thread1,但是log中没有线程1打印的log,可见线程1在等待ing中。接着创建好线程1和主线程,前5次我们只看到主线程和线程2,交替打印log,直到第五次发送满足信号(调用signal()方法),唤醒线程1.过程就是这样简单。

thread2 start!.
This is the main process.
This is thread2.
This is the main process.
This is thread2.
This is the main process.
This is thread2.
This is the main process.
This is thread2.
This is the main process.
This is the main process.
This is thread2. -------------------------线程2第五次打印
thread1 start!.
This is the main process.
This is thread1.   -----线程1唤醒了
This is thread2.
This is the main process.
This is thread2.
This is thread1.
This is the main process.
This is thread2.
This is thread1.
This is the main process.
This is thread2.
This is thread1.
总结:Conditon使用起来还是比较简单的。记住Condition必须配合Mutex来使用,不论是wait,waitRelate,signal,broadcast方法,在实际使用中,都要放在一个mutex.lock

,mutex.unlock()中,当然也可以是一个自动锁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值