BSP Day56

线程同步

所谓同步,即同时起步,协调一致。不同的对象,对“同步”的理解方式各有不同。如,设备同步,是指 在两个设备之间规定一个共同的时间参考:数据库同步,是指让两个或多个数据库内容保持一致,或者按 需要部分保持一致:文件同步,是指让两个或多个文件夹里的文件保持一致。等等。 而,编程中、通信中所说的同步与生活中大家印象中的同步概念略有差异。“同”字应是指协同、协助、 互相配合。主旨在协同步调,按预定的先后次序运行。

同步即协同步调,对公共区域数据按预定的先后次序访问,防止数据混乱。 线程同步,指一个线程发出某一功能调用时, 在没有得到结果之前,该调用不返回。同时其它线程为保 证数据一致性,不能调用该功能。

临界区(Critical Section)、互斥对象(Mutex):主要用于互斥控制;都具有拥有权的控制方法,只有拥有该对象的线程才能执行任务,所以拥有,执行完任务后一定要释放该对象。

信号量(Semaphore)、事件对象(Event):事件对象是以通知的方式进行控制,主要用于同步控制!

临界区:通过对多线程的串行化来访问公共资源或一段代码,速度快,适合控制数据访问。在任意时刻只允许一个线程对共享资源进行访问,如果有多个线程试图访问公共资源,那么在有一个线程进入后,其他试图访问公共资源的线程将被挂起,并一直等到进入临界区的线程离开,临界区在被释放后,其他线程才可以抢占。它并不是核心对象,不是属于操作系统维护的,而是属于进程维护的。

总结下关键段:
1)关键段共初始化化、销毁、进入和离开关键区域四个函数。
2)关键段可以解决线程的互斥问题,但因为具有“线程所有权”,所以无法解决同步问题。
3)推荐关键段与旋转锁配合使用。

2、互斥对象:互斥对象和临界区很像,采用互斥对象机制,只有拥有互斥对象的线程才有访问公共资源的权限。因为互斥对象只有一个,所以能保证公共资源不会同时被多个线程同时访问。当前拥有互斥对象的线程处理完任务后必须将线程交出,以便其他线程访问该资源。

总结下互斥量Mutex:
1)互斥量是内核对象,它与关键段都有“线程所有权”所以不能用于线程的同步。
2)互斥量能够用于多个进程之间线程互斥问题,并且能完美的解决某进程意外终止所造成的“遗弃”问题。


信号量:信号量也是内核对象。它允许多个线程在同一时刻访问同一资源,但是需要限制在同一时刻访问此资源的最大线程数目

在用CreateSemaphore()创建信号量时即要同时指出允许的最大资源计数和当前可用资源计数。一般是将当前可用资源计数设置为最 大资源计数,每增加一个线程对共享资源的访问,当前可用资源计数就会减1 ,只要当前可用资源计数是大于0 的,就可以发出信号量信号。但是当前可用计数减小 到0 时则说明当前占用资源的线程数已经达到了所允许的最大数目,不能在允许其他线程的进入,此时的信号量信号将无法发出。线程在处理完共享资源后,应在离 开的同时通过ReleaseSemaphore ()函数将当前可用资源计数加1 。在任何时候当前可用资源计数决不可能大于最大资源计数。

事件对象: 通过通知操作的方式来保持线程的同步,还可以方便实现对多个线程的优先级比较的操作

总结下事件
1)事件是内核对象,事件分为手动置位事件和自动置位事件。事件Event内部它包含一个使用计数(所有内核对象都有),一个布尔值表示是手动置位事件还是自动置位事件,另一个布尔值用来表示事件有无触发。
2)事件可以由SetEvent()来触发,由ResetEvent()来设成未触发。还可以由PulseEvent()来发出一个事件脉冲。
3)事件可以解决线程间同步问题,因此也能解决互斥问题。

数据混乱原因

1.资源共享(独享资源则不会)

2.调度随机(意味着数据访问会出现竞争)

3.线程间缺乏必要的同步机制 以上

3点中,前两点不能改变,欲提高效率,传递数据,资源必须共享。只要共享资源,就一定会出现竞 争。只要存在竞争关系,数据就很容易出现混乱。 所以只能从第三点着手解决。使多个线程在访问共享资源的时候,出现互斥。

互斥量

Linux中提供一把互斥锁 mutex (也称之为互斥量)。 每个线程在对资源操作前都尝试先加锁,成功加锁才能操作。操作结束解锁。 资源还是共享的,线程间也还是竞争的, 但通过“锁”就将资源的访问变成互斥操作,而后与时间有关的错误也不会再产生了

 但,应注意:同一时刻,只能有一个线程持有该锁。 当A线程对某个全局变量加锁访问,B在访问前尝试加锁,拿不到锁,B阻塞。C线程不去加锁,而直接访 问该全局变量,依然能够访问,但会出现数据混乱。 所以,互斥锁实质上是操作系统提供的一把“建议锁”(又称“协同锁”),建议程序中有多线程访问共享资源 的时候使用该机制。但,并没有强制限定。 因此,即使有了mutex,如果有线程不按规则来访问数据,依然会造成数据混乱。

例程:父子线程共同使用共享资源stdout,利用sleep造成时间差混乱

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
void *tfn(void *arg)
{
srand(time(NULL));
while (1)
{
printf("hello ");
sleep(rand() % 3); /*模拟长时间操作共享资源 ,导致cpu易主,产生与时间有关的错误*/
printf("world\n" );
sleep(rand() % 3);
}
return NULL;
}
int main(void)
{
pthread_t tid;
srand(time(NULL));
pthread_create(&tid, NULL, tfn, NULL);
while (1)
{
printf("HELLO ");
sleep(rand() % 3);
printf("WORLD\n");
sleep(rand() % 3);
}
pthread_join(tid, NULL);
return 0;
}

主要应用函数

pthread_mutex_init 函数。

pthread_mutex_destroy函数。

pthread_mutex_lock 函做。

pthread_mutex_trylock 函数。

pthread_mutex_unlock 函数。

以上5个函数的返回值都是:成功返回0,失败返回错误号。 pthread_mutex_t 类型,其本质是一个结构体。 为简化理解,应用时可忽略其实现细节,简单当成 整数看待。 pthread_mutex_t mutex;变量mutex只有两种取值1、0。

使用mutex(互斥量、互斥锁)一般步骤

1. pthread_mutex_t mutex: 创建锁

2. pthread_mutex_init :初始化

3. pthread_mutex_lock:加锁

4. 访问共享数据(stdout)

5. pthread_mutex_unlock:解锁

6. pthread_mutex_destroy:销毁锁 

lock与unlock:

lock尝试加锁,如果加锁不成功,线程阻塞,阻塞到持有该互斥量的其他线程解锁为止 unlock主动解锁函数。同时将阻塞在该锁上的所有线程全部唤醒,至于哪个线程先被唤醒,取决于优先 级、调度。默认:先阻塞、先唤醒。 例如: T1 T2 T3 T4 使用一把mutex锁。T1加锁成功,其他线程均阻塞,直至T1 解锁。T1解锁后,T2 T3 T4均被唤醒,并自动再次尝试加锁。 可假想mutex锁init 成功初值为1. lock 功能是将mutex--. 而unlock则将mutex++。

lock与trylock

lock加锁失败会阻塞,等待锁释放。

trylock加锁失败直接返回错误号(如: EBUSY)不阻塞

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
//pthread_mutex_t 互斥锁类型名,
pthread_mutex_t mutex;//定义另一个互斥锁变量mutex,全局变量,所有线程可以共享使用
void *tfn2(void *arg)
{//当某个线程,不进行对共享资源进行加锁操作时,仍旧会造成抢夺资源现象
srand(time(NULL));
while(1)
{
int ret=pthread_mutex_trylock(&mutex);
if(ret!=0)
{
printf("pthread_mutx_trylock err:%s\n",strerror(ret));
}
else
{
printf("---tfn2()---print ");
sleep(rand() % 3);
printf("to stdout\n");
sleep(rand() % 3);
ret=pthread_mutex_unlock(&mutex);//解锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_unlock()
error:%s\n",strerror(ret));
}
}
printf("common tfn2()------\n");
sleep(rand() % 3);
}
}
void *tfn(void *arg)
{
srand(time(NULL));
while (1)
{
int ret=pthread_mutex_lock(&mutex);//加锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_lock() error:%s\n",strerror(ret));
}
printf("hello ");
sleep(rand() % 3);
printf("world\n");
//pthread_mutex_unlock(),对加锁成功的共享资源进行解锁
ret=pthread_mutex_unlock(&mutex);//解锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_unlock() error:%s\n",strerror(ret));
}
sleep(rand() % 3);
}
return NULL;
}
int main(void)
{
pthread_t tid;
srand(time(NULL));
//pthread_mutex_init()初始化 互斥锁
int ret=pthread_mutex_init(&mutex,NULL);//初始化锁
if(ret!=0)
{
fprintf(stderr,"mutex init error:%s\n",strerror(ret));
}
//pthread_create() 创建线程1
ret=pthread_create(&tid, NULL, tfn, NULL);
if(ret!=0)
{
fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
}
//pthread_create() 创建线程2
ret=pthread_create(&tid, NULL, tfn2, NULL);
if(ret!=0)
{
fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
}
while (1)
{ //pthread_mutex_lock()给已初始化过的互斥锁变量,(站在当前线程角度)对共享资源进行加
锁操作
//如果加锁失败,则阻塞,也就是一直等待
ret=pthread_mutex_lock(&mutex);//加锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_lock() error:%s\n",strerror(ret));
}
printf("HELLO ");
sleep(rand() % 3);
printf("WORLD\n");
//pthread_mutex_unlock(),对加锁成功的共享资源进行解锁
ret=pthread_mutex_unlock(&mutex);//解锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_unlock() error:%s\n",strerror(ret));
}
sleep(rand() % 3);
}
pthread_join(tid, NULL);
ret=pthread_mutex_destroy(&mutex);//销毁锁
if(ret!=0)
{
fprintf(stderr,"pthread_mutex_destroy() error:%s\n",strerror(ret));
}
return 0;
}

 例程:父子线程共同使用共享资源stdout,利用sleep造成时间差混乱。加锁进行共享资源控制。

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t mutex;
void *tfn(void *arg)
{
srand(time(NULL));
while (1)
{
pthread_mutex_lock(&mutex);//加锁
printf("hello ");
sleep(rand() % 3); /*模拟长时间操作共享资源 ,导致cpu易主,产生与时间有关的错误*/
printf("world\n" );
pthread_mutex_unlock(&mutex);//解锁

sleep(rand() % 3);
}
return NULL;
}
int main(void)
{
pthread_t tid;
srand(time(NULL));
int ret=pthread_mutex_init(&mutex,NULL);//初始化锁
if(ret!=0)
{
fprintf(stderr,"mutex init error:%s\n",strerror(ret));
}
pthread_create(&tid, NULL, tfn, NULL);
while (1)
{
pthread_mutex_lock(&mutex);//加锁
printf("HELLO ");
sleep(rand() % 3);
printf("WORLD\n");
pthread_mutex_unlock(&mutex);//解锁
sleep(rand() % 3);
}
pthread_join(tid, NULL);
pthread_mutex_destroy(&mutex);//销毁锁
return 0;
}

注意事项:

尽量保证锁的粒度,越小越好。 (访问共享数据前, 加锁。访问结束后,应立即解锁)

互斥锁:本质是结构体,我们可以看做是整数,初值为1 (pthread_mutex_init()函数调用成功)

加锁:--操作,阻塞线程。

解锁:++操作,唤醒阻塞在锁上的线程

try锁:尝试加锁,成功--;失败,继续其他业务功能代码 

死锁

是使用锁不恰当导致的现象

1.线程试图对同一个互斥量A加锁两次。(连续调用pthread_mutex_lock两次及以上)

1.读写锁是“写模式加锁”成功时, 其他线程尝加锁都会被阻塞。 2.读写锁是“读模式加锁”成功时,其他线程以写模式加锁会阻塞。 3.读写锁是“ 读模式加锁”时,既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写 锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高. 读写锁也叫共享–独占锁。 a.当读写锁以读模式锁住时,它是以共享模式锁住的,其他读线程可以读取内容 b.当它以写模式锁住时,它是以独占模式锁住的,其他尝试操作的线程均会被阻塞 读写锁非常适合于对数据结构读的次数远大于写的情况。

2.线程1拥有A锁,请求获得B锁:线程2拥有B锁,请求获得A锁。

特别强调。读写锁只有一把,但其具备两种状态: 1.读模式下加锁状态(读锁)。 2.写模式下加锁状态(写锁)。 

读写锁 

与互斥量类似,但读写锁允许更高的并行性。 其特性为:锁只有一把;写独占,读共享;写锁优先级高;

特别强调。读写锁只有一把,但其具备两种状态: 1.读模式下加锁状态(读锁)。 2.写模式下加锁状态(写锁)。

读写锁特征

1.读写锁是“写模式加锁”成功时, 其他线程尝加锁都会被阻塞。

2.读写锁是“读模式加锁”成功时,其他线程以写模式加锁会阻塞。

3.读写锁是“ 读模式加锁”时,既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写 锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高。

读写锁也叫共享–独占锁。

a.当读写锁以读模式锁住时,它是以共享模式锁住的,其他读线程可以读取内容。

b.当它以写模式锁住时,它是以独占模式锁住的,其他尝试操作的线程均会被阻塞 读写锁非常适合于对数据结构读的次数远大于写的情况。

主要应用函数

pthread_rwlock_init 函数。

pthread_rwlock_destroy 函数。

pthread_rwlock_rdlock 函数。

pthread_rwlock_wrlock函数。

pthread_rwlock_tryrdlock 函数。

pthread_rwlock_trywrlock 函数。

pthread_rwlock_unlock函数。

以上7个函数的返回值都是,成功返回0,失败直接返回惜误号 。

pthread_rwlock_t 类型 用于定义一个读写锁变量 如:pthread_rwlock_t rwlock;

初始化一把读写锁

int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);

参1:传读写锁变量

参2:attr表示读写锁属性,通常使用默认值,传NULL即可。

销毁一把读写锁

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

参1:传读写锁变量 

以读方式请求读写锁(简称:请求读锁)

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

参1:传读写锁变量 

以写方式请求读写锁(简称:请求写锁)

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

参1:传读写锁变量 

解锁

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

参1:传读写锁变量 

例程:3个线程不定时写同一全局资源,5个线程不定时读同一全局资源

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int counter;
pthread_rwlock_t rwlock;
/* 3个线程不定时写同一全局资源,5个线程不定时读同一全局资源*/
void *th_write(void *arg)
{
int t;
int i = (long int)arg;
while (1)
{
pthread_rwlock_wrlock(&rwlock);//以写模式加锁
t = counter;
sleep(1);
printf("=====write %d: %lu: counter=%d ++counter=%d\n", i,
pthread_self(), t, ++counter);
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
return NULL;
}
void *th_read(void *arg)
{
int i = (long int)arg;
while (1)
{
pthread_rwlock_rdlock(&rwlock);//读线程间,读锁共享
printf("----------------------read %d: %lu: %d\n", i, pthread_self(),
counter);
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
return NULL;
}
int main(void)
{
long int i;
pthread_t tid[8];
pthread_rwlock_init(&rwlock, NULL);
for(i=0;i<3;i++)
{
pthread_create(&tid[i], NULL, th_write, (void *)i);
}
for(i=0;i<5;i++)
{
pthread_create(&tid[i+3], NULL, th_read, (void *)i);
}
for(i=0;i<8;i++)
{
pthread_join(tid[i], NULL);
}
pthread_rwlock_destroy(&rwlock);
return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixiaxiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值