linux多线程-互斥&条件变量与同步
多线程代码问题描述
我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位。在多线程的程序中,多个线程共享临界区资源,那么就会有问题:
比如
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> int g_val = ;
void * test1(void* args)
{
g_val = ;
printf("in %s: g_val = %d\n",__func__, g_val);
}
void * test2(void* args)
{
sleep();
printf("in %s: g_val = %d\n",__func__,g_val);
}
int main(int argc, char const *argv[])
{
pthread_t id1,id2;
pthread_create(&id1,NULL,test1,NULL);
pthread_create(&id2,NULL,test2,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return ;
}
由次我们可以看到,线程1修改了全局变量,而线程2中页跟着改变了。
那么,对于这个问题进行放大,我们就会找到多线程存在的问题。如下
#include <stdio.h>
#include <pthread.h>
// pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int g_val = ;
void* add(void *argv)
{
for(int i = ; i < ; ++i)
{
// g_val++;
// pthread_mutex_lock(&lock);
int tmp = g_val;
g_val = tmp+;
// pthread_mutex_unlock(&lock);
}
} int main(int argc, char const *argv[])
{
pthread_t id1,id2; pthread_create(&id1,NULL,add,NULL);
pthread_create(&id2,NULL,add,NULL); pthread_join(id1,NULL);
pthread_join(id2,NULL); printf("%d\n",g_val);
return ;
}
在上面代码中,我们执行两个线程分别对全局变量累加5000次,但是得到的结果却是不确定的。这是因为,在多线程程序中,线程调度使得线程间进行切换执行,如果当线程1将数据从内存读入cpu正在准备累加时,调度器切换线程2执行,此时,线程2获取的值是未累加的。那么,当两个线程都执行完本次累加后,实际值只增加了1。所以就会产生多次执行,结果不确定性。
注:代码中没有直接g_val++,而选择了tmp过度就是为了产生非原子操作,让调度过程处于累加未完时。
那么解决这个问题,就需要互斥操作了。
我们首先来谈互斥量mutex
通过互斥量实现线程锁,在每个线程累加之前,进行临界资源的锁操作,在结束时解锁,那么就能保证目标的实现了。
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int g_val = ;
void* add(void *argv)
{
for(int i = ; i < ; ++i)
{
// g_val++;
pthread_mutex_lock(&lock);
int tmp = g_val;
g_val = tmp+;
pthread_mutex_unlock(&lock);
}
} int main(int argc, char const *argv[])
{
pthread_t id1,id2; pthread_create(&id1,NULL,add,NULL);
pthread_create(&id2,NULL,add,NULL); pthread_join(id1,NULL);
pthread_join(id2,NULL); printf("%d\n",g_val);
return ;
}
关于互斥锁的实现,在linux中实现如下
条件变量
问题场景描述
假设我们现在需要做一个生产者消费者模型,生产者对带有头节点的链表头插方式push_front生产数据,消费者调用pop_front消费数据.而生产者可能动作比较慢,这时就会有问题。
生产者生产一个数据时间,消费者可能迫切需求。因此,一直轮寻申请锁资源,以便进行消费。所以就会产生多次不必的锁资源申请释放动作。影响系统性能。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
typedef struct node
{
int _data;
struct node *_next;
}node_t,* node_p,**node_pp; node_p head = NULL; node_p alloc_node(int data)
{
node_p ret = (node_p)malloc(sizeof(node_t));
ret->_data = data;
ret->_next = NULL;
return ret;
} void init(node_pp phead)
{
*phead = alloc_node();
} void push_front(node_p head,int data)
{
node_p tmp = alloc_node(data);
tmp->_next = head->_next;
head->_next = tmp;
} void pop_front(node_p head, int * pdata)
{
if(head->_next!=NULL)
{
node_p tmp = head->_next;
head->_next = tmp->_next; *pdata = tmp->_data;
free(tmp);
}
} void show(node_p head)
{
node_p cur = head->_next;
while(cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
} //消费者
void * consumer(void *argv)
{
int data;
while()
{
pthread_mutex_lock(&lock);
// while(head->_next==NULL)
if(head->_next==NULL)
{
printf("producter is not ready\n");
// pthread_cond_wait(&cond,&lock);
// break;
}
else{
printf("producter is ready...\n");
pop_front(head,&data);
printf("%s data = %d \n",__func__, data);
}
pthread_mutex_unlock(&lock); sleep();
}
} void * producter(void * argv)
{
int data = rand()%;
while()
{
sleep();
pthread_mutex_lock(&lock);
push_front(head,data);
printf("%s data :: %d\n",__func__, data);
pthread_mutex_unlock(&lock);
// pthread_cond_signal(&cond);
}
} int main(int argc, char const *argv[])
{
init(&head); pthread_t id1,id2; pthread_create(&id1,NULL,consumer,NULL);
pthread_create(&id2,NULL,producter,NULL); pthread_join(id1,NULL);
pthread_join(id2,NULL);
}
由上,我们发现。生产者生叉一个数据之后,消费者总是会多次进行锁资源申请并尝试消费数据。那么,解决这一问题的方案就是:条件变量。
具体实现如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
typedef struct node
{
int _data;
struct node *_next;
}node_t,* node_p,**node_pp; node_p head = NULL; node_p alloc_node(int data)
{
node_p ret = (node_p)malloc(sizeof(node_t));
ret->_data = data;
ret->_next = NULL;
return ret;
} void init(node_pp phead)
{
*phead = alloc_node();
} void push_front(node_p head,int data)
{
node_p tmp = alloc_node(data);
tmp->_next = head->_next;
head->_next = tmp;
} void pop_front(node_p head, int * pdata)
{
if(head->_next!=NULL)
{
node_p tmp = head->_next;
head->_next = tmp->_next; *pdata = tmp->_data;
free(tmp);
}
} void show(node_p head)
{
node_p cur = head->_next;
while(cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
} //消费者
void * consumer(void *argv)
{
int data;
while()
{
pthread_mutex_lock(&lock);
while(head->_next==NULL)
// if(head->_next==NULL)
{
printf("producter is not ready\n\n");
pthread_cond_wait(&cond,&lock);
break;
}
// else{
// printf("producter is ready...\n");
pop_front(head,&data);
printf("%s data = %d \n",__func__, data);
// }
pthread_mutex_unlock(&lock); sleep();
}
} void * producter(void * argv)
{
int data = rand()%;
while()
{
sleep();
pthread_mutex_lock(&lock);
push_front(head,data);
printf("%s data :: %d\n",__func__, data);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond); //条件变量v操作
}
} int main(int argc, char const *argv[])
{
init(&head); pthread_t id1,id2; pthread_create(&id1,NULL,consumer,NULL);
pthread_create(&id2,NULL,producter,NULL); pthread_join(id1,NULL);
pthread_join(id2,NULL);
}
由图可以看出,这下我们的消费者不再进行过多次没必要的轮寻访问,当生产者生产数据时,告诉消费者可以进行消费了,那么消费者进行消费。
其实这也就是著名的:好莱坞原则---不要打电话给我们,我们会通知你。
eg,在面试笔试中,我们不需要过度的紧张是否被录用,只需要在做到最大努力之后等着招聘方通知就好。
注:一个Condition Variable总是和一个Mutex搭配使用的。一个线程可以调用
pthread_cond_wait在一一个Condition Variable上阻塞等待,这个函数做以下三步操作:
1. 释放Mutex
2. 阻塞等待
3. 当被唤醒时,重新获得Mutex并返回
linux多线程-互斥&条件变量与同步的更多相关文章
- Linux多线程(三)(同步互斥)
1. 线程的同步与互斥 1.1. 线程的互斥 在Posix Thread中定义了一套专门用于线程互斥的mutex函数.mutex是一种简单的加锁的方法来控制对共享资源的存取,这个互斥锁只有两种状态(上 ...
- Linux系统编程—条件变量
条件变量是用来等待线程而不是上锁的,条件变量通常和互斥锁一起使用.条件变量之所以要和互斥锁一起使用,主要是因为互斥锁的一个明显的特点就是它只有两种状态:锁定和非锁定,而条件变量可以通过允许线程阻塞和等 ...
- 27 python 初学(信号量、条件变量、同步条件、队列)
参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html semaphore 信号量: condition 条件变量: event 同步条件:条件 ...
- OS: 读者写者问题(写者优先+LINUX+多线程+互斥量+代码)(转)
一. 引子 最近想自己写个简单的 WEB SERVER ,为了先练练手,熟悉下在LINUX系统使用基本的进程.线程.互斥等,就拿以前学过的 OS 问题开开刀啦.记得当年学读者写者问题,尤其是写者优先的 ...
- linux中的条件变量
1 大家可能知道互斥量是线程程序中必须的工具了,但是也不能是万能的,就比如某个线程正在等待共享数据某个条件的发生,这个时候会发生什么呢.它就可能重复的尝试对互斥对象锁定和解锁来检查共享数据结构. 2 ...
- POSIX多线程编程-条件变量pthread_cond_t
条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用.使用时,条件变量被用来阻塞一个线程,当条件不满足时,线程往往解开相应的互斥锁并等待条件发生变化.一旦其它 ...
- 【Linux多线程】三个经典同步问题
在了解了<同步与互斥的区别>之后,我们来看看几个经典的线程同步的例子.相信通过具体场景可以让我们学会分析和解决这类线程同步的问题,以便以后应用在实际的项目中. 一.生产者-消费者问题 问题 ...
- Linux Posix线程条件变量
生产者消费者模型 .多个线程操作全局变量n,需要做成临界区(要加锁--线程锁或者信号量) .调用函数pthread_cond_wait(&g_cond,&g_mutex)让这个线程锁在 ...
- Linux 多线程互斥量互斥
同步 同一个进程中的多个线程共享所在进程的内存资源,当多个线程在同一时刻同时访问同一种共享资源时,需要相互协调,以避免出现数据的不一致和覆盖等问题,线程之间的协调和通信的就叫做线程的同步问题, 线程同 ...
随机推荐
- List的遍历和删除元素
package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...
- 提高c++性能的编程技术笔记
需要时再创建对象,比如在类中用if new 而不是在构造函数里创建类的成员. 用char 指针而不是string可以节省构造和析构string的开销. 虚函数无法内联的性能损失.
- git 在提交之前撤销add操作
问题 在使用git时,在未添加.ignore文件前使用 git add . 将所有文件添加到库中,不小心将一些不需要加入版本库的文件加到了版本库中.由于此时还没有提交所以不存在HEAD版本,不能使用 ...
- java之内部类(InnerClass)----非静态内部类、静态内部类、局部内部类、匿名内部类
提起java内裤类(innerClass)很多人不太熟悉,实际上类似的概念在c++里面也有,那就是嵌套类(Nested Class),关于这俩者的区别,在下文中会有对比.内部类从表面上看,就是在类中定 ...
- Cannot open connection 解决办法
试了很多种网上找的办法,都不行,最后才发现是我的beans.xml中完全把下面 这一段代码给遗忘了,忘记写了.添加我就ok了. 我能说花了我近1个小时吗?坑姐哦! <bean class=&qu ...
- devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)
介绍一些常用的gridcontrol设置. 1.设置隔行变色.首先设置显示隔行变色,步骤:OptionsView-->EnableAppearanceEvenRow-->true和Opti ...
- 【Machine Learning】机器学习の特征
绘制了一张导图,有不对的地方欢迎指正: 下载地址 机器学习中,特征是很关键的.其中包括,特征的提取和特征的选择.他们是降维的两种方法,但又有所不同: 特征抽取(Feature Extraction): ...
- Lambda动态创建
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [Shell] 读取脚本路径
以下是几种在 Shell 中读取路径的方法. 返回当前工作目录绝对路径 echo $(pwd) 返回 shell 第一个参数.如果被执行对象位于 PATH 路径中,则返回该对象绝对路径:否则返回被执行 ...
- apple Swift教程大全,希望对你有帮助!
1)apple Swift编程入门文档- http://gashero.iteye.com/blog/2075324 一位大神写的关于Swift的一些介绍和简单的使用,里面介绍了Swift和其他语言的 ...