<div class="cnblogs_code">
#includepthread_mutex_t chopstick[] ; *eat_think( * phi = *( *)arg; left,right; = = = = = = = = = = </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i; </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(;;){ usleep(</span><span style="color: #800080;">3</span>); <span style="color: #008000;">//</span><span style="color: #008000;">思考,将进程挂起一段时间</span> pthread_mutex_lock(&chopstick[left]); <span style="color: #008000;">//</span><span style="color: #008000;">拿起左手的筷子,锁定互斥量,加锁</span> printf(<span style="color: #800000;">"</span><span style="color: #800000;">Philosopher %c fetches chopstick %d\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,phi,left);<a href="/tag/shuchu/" target="_blank" class="keywords">输出</a>哲学家拿起了左手边的筷子 </span><span style="color: #0000ff;">if</span> (pthread_mutex_trylock(&chopstick[right]) == EBUSY){ <span style="color: #008000;">//</span><span style="color: #008000;">判断右手的筷子是否有人用,再试一次获得对互斥量的锁定(非阻塞) </span> pthread_mutex_unlock(&chopstick[left]); <span style="color: #008000;">//</span><span style="color: #008000;">如果右边筷子被拿走放下左手的筷子,解锁互斥量,解锁</span> <span style="color: #0000ff;">continue</span>;<span style="color: #008000;">//</span><span style="color: #008000;">如果此哲学家没能吃饭,验证下一个哲学家是否能吃饭,即跳出本次循环进行下次循环</span>
<span style="color: #000000;"> }
</span><span style="color: #008000;">//</span><span style="color: #008000;"> pthread_mutex_lock(&chopstick[right]); </span><span style="color: #008000;">//</span><span style="color: #008000;">拿起右手的筷子,如果想观察死锁,把上一句if注释掉,再把这一句的注释去掉</span>
printf(<span style="color: #800000;">"</span><span style="color: #800000;">Philosopher %c fetches chopstick %d\n</span><span style="color: #800000;">"</span>,right); <span style="color: #008000;">//</span><span style="color: #008000;"><a href="/tag/shuchu/" target="_blank" class="keywords">输出</a>此哲学家又拿起了右手边的跨子</span>
printf(<span style="color: #800000;">"</span><span style="color: #800000;">Philosopher %c is eating.\n</span><span style="color: #800000;">"</span>,phi);<span style="color: #008000;">//</span><span style="color: #008000;"><a href="/tag/shuchu/" target="_blank" class="keywords">输出</a>此次的哲学家拿起啦一双筷子在吃饭</span>
usleep(<span style="color: #800080;">3</span>); <span style="color: #008000;">//</span><span style="color: #008000;">吃饭,把进程挂起一段时间 </span>
pthread_mutex_unlock(&chopstick[left]); <span style="color: #008000;">//</span><span style="color: #008000;">放下左手的筷子</span>
printf(<span style="color: #800000;">"</span><span style="color: #800000;">Philosopher %c release chopstick %d\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,left);
pthread_mutex_unlock(</span>&chopstick[right]); <span style="color: #008000;">//</span><span style="color: #008000;">放下右手的筷子</span>
printf(<span style="color: #800000;">"</span><span style="color: #800000;">Philosopher %c release chopstick %d\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,right);
}
}
<span style="color: #0000ff;">int<span style="color: #000000;"> main(){
pthread_t A,B,C,D,E; <span style="color: #008000;">//<span style="color: #008000;">5个哲学家
<span style="color: #0000ff;">int</span><span style="color: #000000;"> i;
</span><span style="color: #0000ff;">for</span> (i = <span style="color: #800080;">0</span>; i < <span style="color: #800080;">5</span>; i++<span style="color: #000000;">)
pthread_mutex_init(</span>&chopstick[i],NULL);<span style="color: #008000;">//</span><span style="color: #008000;">初始化默认互斥锁<a href="/tag/shuxing/" target="_blank" class="keywords">属性</a>的互斥锁数组chopstick[i],默认<a href="/tag/shuxing/" target="_blank" class="keywords">属性</a>为<a href="/tag/kuaisu/" target="_blank" class="keywords">快速</a>互斥锁</span>
pthread_create(&A,NULL,eat_think,<span style="color: #800000;">"</span><span style="color: #800000;">A</span><span style="color: #800000;">"</span>);<span style="color: #808080;">///</span><span style="color: #008000;">/创建并<a href="/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>到线程<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>创建并<a href="/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>到参数为指向线程标识符的指针为 A 线程<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>eat_think,A是运行<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>的参数</span>
pthread_create(&B,<span style="color: #800000;">"</span><span style="color: #800000;">B</span><span style="color: #800000;">"</span><span style="color: #000000;">);
pthread_create(</span>&C,<span style="color: #800000;">"</span><span style="color: #800000;">C</span><span style="color: #800000;">"</span><span style="color: #000000;">);
pthread_create(</span>&D,<span style="color: #800000;">"</span><span style="color: #800000;">D</span><span style="color: #800000;">"</span><span style="color: #000000;">);
pthread_create(</span>&E,<span style="color: #800000;">"</span><span style="color: #800000;">E</span><span style="color: #800000;">"</span><span style="color: #000000;">);
pthread_join(A,NULL);</span><span style="color: #008000;">//</span><span style="color: #008000;">等待线程标识符为 A 的eat_think线程<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>结束</span>
<span style="color: #000000;"> pthread_join(B,NULL);
pthread_join(C,NULL);
pthread_join(D,NULL);
pthread_join(E,NULL);
<span style="color: #0000ff;">return <span style="color: #800080;">0<span style="color: #000000;">;
}