目录
Linux 线程同步和互斥:互斥锁
互斥锁概念
互斥锁分类
实现步骤
互斥锁初始化函数:pthread_mutex_init
互斥锁上锁函数:pthread_mutex_lock
互斥锁解锁函数:pthread_mutex_unlock
消除互斥锁函数:pthread_mutex_destroy
Linux 线程同步和互斥:条件变量
条件变量相关概念
条件变量的创建和销毁
条件变量的使用
互斥锁判断上锁:pthread_mutex_trylock
int pthread_mutex_destroy( pthread_mutex_t * mutex);
/*
主线程:用死循环完成对循环变量i++计数并赋值给两个全局变量vl1,vl2
子线程:判断是否vl1和vl2是否相等,不等打印两个变量的值
*/
#include
#include
#include
#include
int globa_v1,globa_v2;
//pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //创建互斥锁对象
pthread_mutex_t mutex ; //创建互斥锁对象
void * thread(void *arg)
{ //线程体:并发执行代码的入口这里开始
while (1) {
pthread_mutex_lock(&mutex);
if (globa_v1 != globa_v2)
printf("globa_v1=%d, globa_v2=%d\n", globa_v1, globa_v2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
}
int main(void)
{
pthread_t tid;
int i;
pthread_create(&tid, NULL, thread, NULL);
for(i=0;; i++){
pthread_mutex_lock(&mutex);
globa_v1=i+1;
//时间片切换出去了,出现数据不一致
globa_v2=i+1;
pthread_mutex_unlock(&mutex);
}
//等待线程结束,回收线程资源
pthread_join(tid, NULL);
return 0;
}
/*
进程1循环计数15次,每到3的倍数,调用条件通知函数,发信号唤醒进程2
进程2判断循环计数是否为3的倍数,是调用条件等待函数,阻塞进程2
需要用到两个对象:互斥锁对象 条件变量对象
*/
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
int i=1;
void * thread1(void *arg)
{ //线程体:并发执行代码的入口这里开始
pthread_detach(pthread_self()); //分离线程,不用join回收
for (i=1; i<=15; i++) {
if (i%3==0) usleep(200000); //200ms 确保要得到通知的线程进入一个阻塞等待通知状态
pthread_mutex_lock(&mutex); //上锁
if (i % 3 ==0) { //条件测试
pthread_cond_signal(&cond); //条件成立,发送信号给进程2
} else {
printf("thread1: %d\n", i);
}
pthread_mutex_unlock(&mutex); //解锁
usleep(500000);
}
pthread_exit(0);
}
void * thread2(void *arg)
{ //线程体:并发执行代码的入口这里开始
while(i<15) {
pthread_mutex_lock(&mutex); //上锁
if(i%3 == 0) {
pthread_cond_wait(&cond, &mutex); // 阻塞,直到等待信号通知我,解除阻塞
printf("thread2:%d\n", i);
}
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(0);
}
int main(void)
{
pthread_t tid1,tid2;
//初始化互斥锁和条件变量
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("main thread : start\n");
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
//等待线程结束,回收线程资源
pthread_join(tid2, NULL);
printf("main thread : over\n");
//销毁两个对象
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return 0;
}
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
void *thread1(void *);
void *thread(void *);
int i=1;
int main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_t t_c;
pthread_t t_d;
int b=2,c=3,d=4;
/*初始化互斥锁和条件变量*/
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("main thread: work start\n");
pthread_create(&t_a,NULL,thread1, NULL);/*创建进程t_a*/
pthread_create(&t_b,NULL,thread, &b); /*创建进程t_b*/
pthread_create(&t_c,NULL,thread, &c);/*创建进程t_c*/
pthread_create(&t_d,NULL,thread, &d); /*创建进程t_d*/
pthread_join(t_a, NULL);/*等待进程t_a结束*/
printf("main: work finished!\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void *thread1(void *arg)
{
for(i=1;i<=15;i++) {
if(i%3 == 0)usleep(200000);//sleep0.2s,确保想要得到我的信号通知的线程都进入一个阻塞等待通知的状态去
pthread_mutex_lock(&mutex);/*锁住互斥量*/
if(i%3 == 0) {
//pthread_cond_signal(&cond);/*条件改变,给某个线程发送单个信号,t_b, t_c,t_d谁能收到不知道*/
pthread_cond_broadcast(&cond);
}
else
printf("thead1:%d\n",i);
pthread_mutex_unlock(&mutex);/*解锁互斥量*/
sleep(1);
}
return NULL;
}
void *thread(void *arg)
{
int no=*((int *)arg);
pthread_detach(pthread_self()); //分离线程
while(i<15) {
pthread_mutex_lock(&mutex); //加锁
if(i%3 == 0) {
pthread_cond_wait(&cond,&mutex);/*阻塞等待“信号”通知*/
printf("thread%d:%d\n", no, i);
}
pthread_mutex_unlock(&mutex); //解锁
usleep(200000);//sleep 0.2s
}
return NULL;
}