/*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);
* 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
可见,只有等到主进程解锁后,子线程才能运行