mutex ----只有等到主进程解锁后,子线程才能运行

本文展示了一个使用pthread互斥锁实现线程同步的C语言示例程序。该程序创建了一个子线程,并通过互斥锁让主线程与子线程交替运行。主线程先锁定互斥锁并运行三次后释放锁,接着子线程获取锁并运行三次,展示了互斥锁的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*mutex.c
 * function:mutex test in main,pthread
 */
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<error.h>


void * pthread_fuction(void *arg);
char flag=1;
pthread_mutex_t mutex;//definite a mutex
int main(int argc,char **argv)
{

    pthread_t pth_id;
int count1=0;
printf( "This in the main :ID is  %lu\n" , pthread_self() );
//initial mutex
if(pthread_mutex_init(&mutex,NULL)!=0)

perror("Mutex initial error");
exit(1);
}
//creat pthread
        if(pthread_create(&pth_id,0,pthread_fuction,NULL)!=0)
{
perror("Thread Create error");
exit(1);
}
//lock mutex
if(pthread_mutex_lock(&mutex)!=0)
{
perror("Mutex lock error");
exit(1);
}
else
printf("Main process Lock!\n");
while(count1++<3)
{
if(flag==1)
{
printf("In Main thread is running\n");
flag=2;
}
else
{
printf("Main thread is sleeping \n");
sleep(1);
}
}
printf("main thread sleep 5 s ,then start unlock\n ");
// printf("main thread then start unlock\n ");
sleep(5);
/* if(pthread_mutex_unlock(&mutex)!=0)
{
perror("Mutex unlock Failed");
exit(1);
}
else
*/ //printf("Main thread unlock\n");
if(pthread_mutex_unlock(&mutex)==0)
printf("Main thread unlock success\n");
else
{
perror("main thread unlock fail");
exit(1);
}
//restore mutex
pthread_mutex_destroy(&mutex);


pthread_join(pth_id,NULL);//wait child thread exit
return 0;
}
void * pthread_fuction(void *arg)
{
char count2=0;
printf( "This in the child thread  :ID is  %lu\n" , pthread_self() );
//add 
sleep(1);//let main process run 
if(pthread_mutex_lock(&mutex)!=0)
// if(pthread_mutex_trylock(&mutex)!=0)
         {
                 perror("Mutex lock error");
                 exit(1);
         }
         else
        printf("Child thread  process Lock!\n");
         while(count2++<3)
        {
                 if(flag==2)
                 {
                         printf("In Child thread is running\n");
                         flag=1;
                 }
                 else
                 {
                         printf("child  thread is sleeping \n");
                       sleep(1);
                 }
         }
 
         if(pthread_mutex_unlock(&mutex)!=0)
         {
                 perror("Mutex unlock Failed");
                 exit(1);
         }
else
printf("child thread unlock\n");
pthread_exit(NULL);

}

运行结果:

 ./mutex
This in the main :ID is  3086722752
This in the child thread  :ID is  3086719888
Main process Lock!
In Main thread is running
Main thread is sleeping 
Main thread is sleeping 
main thread sleep 5 s ,then start unlock
 Child thread  process Lock!
In Child thread is running
child  thread is sleeping 
Main thread unlock success
child  thread is sleeping 
child thread unlock

可见,只有等到主进程解锁后,子线程才能运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值