<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[linux 多线程函数 pthread_cond_wait 及pthread_cond_wait()用法分析。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Tue, 20 Oct 2015 03:28:38 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	最近找到一篇很好的文章将linux多线程函数pthread_cond_wait，是我茅塞顿开，豁然开朗，决定转载过来，以便经常复习记忆。<br/> <br/>条件变量的结构为pthread_cond_t，函数pthread_cond_init（）被用来初始化一个条件变量。它的原型为：<br/>extern int pthread_cond_init __P ((pthread_cond_t *__cond,__const pthread_condattr_t *__cond_attr));<br/>其中cond是一个指向结构pthread_cond_t的指针，cond_attr是一个指向结构pthread_condattr_t的指针。结构 pthread_condattr_t是条件变量的属性结构，和互斥锁一样我们可以用它来设置条件变量是进程内可用还是进程间可用，默认值是 PTHREAD_ PROCESS_PRIVATE，即此条件变量被同一进程内的各个线程使用。注意初始化条件变量只有未被使用时才能重新初始化或被释放。释放一个条件变量的函数为pthread_cond_destroy（pthread_cond_t cond）。　<br/>也可以静态的初始化条件变量<br/>pthread_cond_t my_condition = PTHREAD_COND_INITIALIZER;<br/>函数pthread_cond_wait（）使线程阻塞在一个条件变量上。它的函数原型为：<br/>extern int pthread_cond_wait __P ((pthread_cond_t *__cond,pthread_mutex_t *__mutex));<br/>调用这个函数时,线程解开mutex指向的锁并被条件变量cond阻塞。线程可以被函数pthread_cond_signal和函数 pthread_cond_broadcast唤醒线程被唤醒后，它将重新检查判断条件是否满足，如果还不满足，一般说来线程应该仍阻塞在这里，被等待被下一次唤醒。这个过程一般用while语句实现。<br/>通过下面的程序来理解:<br/>__________________华丽的CODE分割线_________________________<br/>pthreadCondWait.c <br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;&nbsp;&nbsp;
#include &lt;unistd.h&gt; 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;&nbsp;&nbsp;//初始化条件变量 

void *thread1(void *);
void *thread2(void *);

int i=1;
int main(void)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_t t_a;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_t t_b;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_create(&amp;t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_create(&amp;t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_join(t_b, NULL);/*等待进程t_b结束*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_destroy(&amp;mutex);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_destroy(&amp;cond);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);
&#125;

void *thread1(void *junk)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=9;i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_lock(&amp;mutex);//
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(i%3==0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_cond_signal(&amp;cond);/*条件改变，发送信号，通知t_b进程*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;thead1:%d&#92;n&quot;,i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&amp;mutex);//*解锁互斥量*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Up Unlock Mutex&#92;n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&#125;

void *thread2(void *junk)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(i&lt;9)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_lock(&amp;mutex);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(i%3!=0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_cond_wait(&amp;cond,&amp;mutex);/*等待*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;thread2:%d&#92;n&quot;,i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&amp;mutex);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Down Ulock Mutex&#92;n&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&#125;

</textarea><br/><br/>___________________至关重要的绚烂的结果分割线_________________<br/>[root@iZ25dcp92ckZ testdemo]# vi pthreadCondWait.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>[root@iZ25dcp92ckZ testdemo]# gcc pthreadCondWait.c -lpthread -o pthreadCondWaitStudy<br/>[root@iZ25dcp92ckZ testdemo]# ./pthreadCondWaitStudy <br/>thead1:1<br/>Up Unlock Mutex<br/>thead1:2<br/>Up Unlock Mutex<br/>Up Unlock Mutex<br/>thread2:3<br/>Down Ulock Mutex<br/>thread2:3<br/>Down Ulock Mutex<br/>thead1:4<br/>Up Unlock Mutex<br/>thead1:5<br/>Up Unlock Mutex<br/>Up Unlock Mutex<br/>thread2:6<br/>Down Ulock Mutex<br/>thread2:6<br/>Down Ulock Mutex<br/>thead1:7<br/>Up Unlock Mutex<br/>thead1:8<br/>Up Unlock Mutex<br/>Up Unlock Mutex<br/>thread2:9<br/>Down Ulock Mutex<br/><br/>_________________HOW IT WORKS________<br/><br/>i不是三的倍数的时候.<br/>thread2条件变量阻塞,释放Mutex<br/>thread1加锁,打印thread1:i,释放锁,打印&quot;Up Unlock Mutex&quot;<br/>i为3的倍数的时候,<br/>thread1,加锁,条件变量通知,唤醒条件变量阻塞线程,打印&quot;Up Unlock Mutex&quot;<br/>thread2,被唤醒,加锁,打印&quot;thread2:i&quot;,释放锁,打印&quot;Down Ulock Mutex&quot;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;所以说函数pthread_cond_wait()调用时,不仅对条件变量起作用,还对互斥锁有作用!<br/><br/>++++++++++++++++唯美的好书推荐线++++++++++++<br/>&quot;Beiginning Linux Programming&quot; by Neil Matthrew &amp; Richard Stones<br/>You can choose the English version or Chinese version, but I recommend the English one. Maybe it&#039; easier to understand what the authers say than the Chinese one ,&nbsp;&nbsp;even if you&#039;re Chinese.<br/> <br/>原文地址：http://blogold.chinaunix.net/u1/53448/showart_431857.html
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] linux 多线程函数 pthread_cond_wait 及pthread_cond_wait()用法分析。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>