}
else
printf(“function lock\n”);
while(print_count2++<5)
{
if(run_now==2) /分进程:如果run_now为1就把它修改为1*/
{
printf(“function thread is run\n”);
run_now=1;
}
else
{
printf(“function thread is sleep\n”);
sleep(1);
}
}
if(pthread_mutex_unlock(&work_mutex)!=0) /*对互斥量解锁*/
{
perror(“unlock failed”);
exit(1);
}
else
printf(“function unlock\n”);
pthread_exit(NULL);
}
下面是运行结果:
main lock
main thread is run
main thread is sleep
main thread is sleep
main thread is sleep
main thread is sleep
main unlock
function lock
function thread is run
function thread is sleep
function thread is sleep
function thread is sleep
function thread is sleep
function unlock
我们从运行结果可以看到,当主进程把互斥量锁住后,子进程就不能对共享资源进行操作了
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-36452-5.html
巴菲特
质检的买到假东西来检验