mutex的使用_mutex semaphore区别_c mutex使用(5)

电脑杂谈  发布时间:2017-03-08 13:06:44  来源:网络整理

}

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

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...