多线程编程--对pthread_cond_wait()函数的理解
来源:互联网 发布:php九九乘法表4个角度 编辑:程序博客网 时间:2024/05/19 02:43
/************pthread_cond_wait()的使用方法**********/
pthread_mutex_lock(&qlock);
pthread_cond_wait(&qready, &qlock);
pthread_mutex_unlock(&qlock);
/*****************************************************/
The mutex passed to pthread_cond_wait protects the condition.The caller
passes it locked to the function, which then atomically places the
calling thread on the list of threads waiting for the condition and
unlocks the mutex. This closes the window between the time that the
condition is checked and the time that the thread goes to sleep waiting
for the condition to change, so that the thread doesn't miss a change
in the condition. When pthread_cond_wait returns, the mutex is again
locked.
上面是APUE的原话,就是说pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t
*mutex)函数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻
塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先锁
互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得
其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加锁。
实际上边代码的加解锁过程如下:
/************pthread_cond_wait()的使用方法**********/
pthread_mutex_lock(&qlock); /*lock*/
pthread_cond_wait(&qready, &qlock); /*block-->unlock-->wait() return-->lock*/
pthread_mutex_unlock(&qlock); /*unlock*/
pthread_mutex_lock(&qlock);
pthread_cond_wait(&qready, &qlock);
pthread_mutex_unlock(&qlock);
/*****************************************************/
The mutex passed to pthread_cond_wait protects the condition.The caller
passes it locked to the function, which then atomically places the
calling thread on the list of threads waiting for the condition and
unlocks the mutex. This closes the window between the time that the
condition is checked and the time that the thread goes to sleep waiting
for the condition to change, so that the thread doesn't miss a change
in the condition. When pthread_cond_wait returns, the mutex is again
locked.
上面是APUE的原话,就是说pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t
*mutex)函数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻
塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先锁
互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得
其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加锁。
实际上边代码的加解锁过程如下:
/************pthread_cond_wait()的使用方法**********/
pthread_mutex_lock(&qlock); /*lock*/
pthread_cond_wait(&qready, &qlock); /*block-->unlock-->wait() return-->lock*/
pthread_mutex_unlock(&qlock); /*unlock*/
/*****************************************************/
补充: 条件变量的改变,会"依赖于"互斥量
- 多线程编程--对pthread_cond_wait()函数的理解
- 多线程编程--对pthread_cond_wait()函数的理解
- 对pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解
- pthread_cond_wait()函数的理解和pthread_cond_signal函数
- 多线程编程之pthread_cond_wait
- linux多线程函数pthread_cond_wait
- 对JAVA多线程 并发编程的理解
- 对JAVA多线程 并发编程的理解
- 对多线程网络编程的理解
- pthread_cond_wait()的理解
- pthread_cond_wait的理解
- POSIX多线程中的pthread_cond_wait() 函数
- POSIX多线程中的pthread_cond_wait() 函数
- 关于android中的一些textview的span
- MBR分析-----SD卡
- 写一个块设备驱动 1
- 写一个块设备驱动 2
- jquery的ajax教训
- 多线程编程--对pthread_cond_wait()函数的理解
- 弹性动画
- Nginx Location 指令基础
- Android自定义View实现
- POJ 1222 高斯消元法
- mogodb capped collection
- 写代码犹如写文章
- autotools & makefile
- Google 2/3搜索广告收入来自iOS设备 而非Android