C++详解多线程中的线程同步与互斥量

Karli ·
更新时间:2024-09-20
· 1427 次阅读

目录

线程同步

互斥量

线程同步 /* 使用多线程实现买票的案例。 有3个窗口,一共是100张票。 */ #include <stdio.h> #include <pthread.h> #include <unistd.h> // 全局变量,所有的线程都共享这一份资源。 int tickets = 100; void * sellticket(void * arg) { // 卖票 while(tickets > 0) { usleep(6000);//微秒 printf("%ld 正在卖第 %d 张门票\n", pthread_self(), tickets); tickets--; } return NULL; } int main() { // 创建3个子线程 pthread_t tid1, tid2, tid3; pthread_create(&tid1, NULL, sellticket, NULL); pthread_create(&tid2, NULL, sellticket, NULL); pthread_create(&tid3, NULL, sellticket, NULL); // 回收子线程的资源,默认阻塞状态 pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_join(tid3, NULL); // 设置线程分离。 // pthread_detach(tid1); // pthread_detach(tid2); // pthread_detach(tid3); pthread_exit(NULL); // 退出主线程 return 0; }

运行结果:

产生的问题:

每一张票都卖了好几次;

卖了第0张票和-1张票,都是不对的;

三个线程抢cpu资源。

原因: 休眠的时间,3个进程都进去执行了程序;多个进程对共享资源同时处理,就会产生线程同步问题。

线程的主要优势: 通过全局变量来共享信息。不过,这种便捷的共享是有代价的:必须确保多个线程不会同时修改同一变量,或者某一线程不会读取正在由其他线程修改的变量。

临界区:指访问某一共享资源的代码片段,并且这段代码的执行应为原子操作,也就是同时访问同一共享资源的其他线程不应终端该片段的执行。

线程同步: 即当有一个线程在对内存进行操作时,其他线程都不可以对这个内存地址进行操作,直到该线程完成操作,其他线程才能对该内存地址进行操作,而其他线程则处于等待状态。

线程同步会降低线程并发的效率,这是必须的。

互斥量

为避免线程更新共享变量时出现问题,可以使用互斥量(mutex)来确保同时仅有一个线程可以访问某项共享资源。可以使用互斥量来保证对任意共享资源的原子访问。

互斥量有两种状态:已锁定(locked)和未锁定(unlocked)。

任何时候,至多只有一个线程可以锁定该互斥量。试图对已经锁定的某一互斥量再次加锁,将可能阻塞线程或者报错失败,具体取决于加锁时使用的方法。

一旦线程锁定互斥量,随即成为该互斥量的所有者,只有所有者才能给互斥量解锁。

一般情况下,对每一共享资源(可能由多个相关变量组成)会使用不同的互斥量,每一线程在访问同一资源时将采用如下协议:

针对共享资源锁定互斥量

访问共享资源

对互斥量解锁

加锁是在临界区对共享数据操作;

如果多个线程试图执行这一块代码(一个临界区),事实上只有一个线程能够持有该互斥量(其他线程将遭到阻塞),即同时只有一个线程能够进入这段代码区域,如下图所示:

/* 互斥量的类型 pthread_mutex_t int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); - 初始化互斥量 - 参数 : - mutex : 需要初始化的互斥量变量 - attr : 互斥量相关的属性,NULL - restrict : C语言的修饰符,被修饰的指针,不能由另外的一个指针进行操作。 pthread_mutex_t *restrict mutex = xxx; pthread_mutex_t * mutex1 = mutex;--不能这样操作,有restrict关键词 int pthread_mutex_destroy(pthread_mutex_t *mutex); - 释放互斥量的资源 int pthread_mutex_lock(pthread_mutex_t *mutex); - 加锁,阻塞的,如果有一个线程加锁了,那么其他的线程只能阻塞等待 - 成功返回 0 int pthread_mutex_trylock(pthread_mutex_t *mutex); - 尝试加锁,如果加锁失败,不会阻塞,会直接返回。 int pthread_mutex_unlock(pthread_mutex_t *mutex); - 解锁 */ #include <stdio.h> #include <pthread.h> #include <unistd.h> // 全局变量,所有的线程都共享这一份资源。 int tickets = 1000; // 创建一个互斥量--在全局区创建 pthread_mutex_t mutex; void * sellticket(void * arg) { // 卖票 while(1) { // 加锁 pthread_mutex_lock(&mutex); if(tickets > 0) { usleep(6000); printf("%ld 正在卖第 %d 张门票\n", pthread_self(), tickets); tickets--; }else { // 解锁 pthread_mutex_unlock(&mutex); break; } // 解锁 pthread_mutex_unlock(&mutex); } return NULL; } int main() { // 初始化互斥量 pthread_mutex_init(&mutex, NULL); // 创建3个子线程 pthread_t tid1, tid2, tid3; pthread_create(&tid1, NULL, sellticket, NULL); pthread_create(&tid2, NULL, sellticket, NULL); pthread_create(&tid3, NULL, sellticket, NULL); // 回收子线程的资源,阻塞 pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_join(tid3, NULL); pthread_exit(NULL); // 退出主线程 // 释放互斥量资源 pthread_mutex_destroy(&mutex); return 0; }

到此这篇关于C++详解多线程中的线程同步与互斥量的文章就介绍到这了,更多相关C++线程同步内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



线程同步 c+ 互斥 C++ 多线程 线程

需要 登录 后方可回复, 如果你还没有账号请 注册新账号