mutex ----只有等到主进程解锁后,子线程才能运行
来源:互联网 发布:软件测试流程规范化 编辑:程序博客网 时间:2024/05/21 11:17
/*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
可见,只有等到主进程解锁后,子线程才能运行
- mutex ----只有等到主进程解锁后,子线程才能运行
- SB了,pthread_cond_wait(),在等待的时候会解锁(阻塞之后,解锁mutex给别的线程),唤醒后加锁,
- C#利用Mutex互斥量实现同时只有一个进程实例在运行
- System.Threading.Mutex:一台电脑上面只有一个进程实例在运行,利用Mutex互斥量可以实现了这个功能
- 使用互斥体Mutex 实现应用程序只有一个实例运行
- 为什么fork创建子进程后,父进程中运行过的代码在子进程中不再运行了
- android--------------只有activity才能产生子窗口----------context不行
- 原来只有状态为'Ready'的进程才能收到信号
- 只有80后才能懂得的生活规则
- 只有80后才能懂得的生活规则
- DIV弹出后只有关了才能点网页其他地方
- 线程、进程通信---mutex=>互斥锁
- 只有偏执狂才能生存!
- 只有偏执狂才能生存
- 只有战斗 才能活着
- 只有偏执狂才能生存!
- 进程解锁
- CountDownLatch实现主线程等待所有子线程运行结束后再继续执行的实现
- 养成好习惯应对猖獗盗刷
- 黑马程序员 数据库一些基础使用
- Chromium音频输出相关简介 - AudioPlayback support in Chromium
- Understanding Ajax vulnerabilities
- constructor的执行顺序
- mutex ----只有等到主进程解锁后,子线程才能运行
- windows 下保存shell文件为可以在Linux下运行的格式
- javascript验证身份证完全方法.
- From LOW to PWNED [3] JBoss/Tomcat server-status
- Xcode 4.3 Three20 安装 320
- Flex创建AIR项目
- Basic drag and drop in WinForms
- 后缀数组
- Q_SIGNALS,Q_SLOTS, Q_EMIT