原文链接:http://blog.sina.com.cn/s/blog_967817f20101bsf0.html
参考链接:http://www.cnblogs.com/Dahaka/archive/2012/02/19/2358528.html
因为pthread_cond_wait函数包含了很多操作,在wait之前和之后必须要手动加锁/解锁mutex,保证同一时间只有一个人对条件变量cond进行使用。
wait函数开始和结束的地方有解锁/加锁mutex,下面贴出源码:
android-2.2.2_r1ioniclibcionicpthread.c
1 int __pthread_cond_timedwait_relative(pthread_cond_t *cond, 2 pthread_mutex_t * mutex, 3 const struct timespec *reltime) 4 { 5 int status; 6 int oldvalue = cond->value; 7 int wait_op = COND_IS_SHARED(cond) ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE; 8 9 pthread_mutex_unlock(mutex); 10 status = __futex_syscall4(&cond->value, wait_op, oldvalue, reltime); 11 pthread_mutex_lock(mutex); 12 13 if (status == (-ETIMEDOUT)) return ETIMEDOUT; 14 return 0; 15 }