linux多线程之互斥锁
目录
互斥锁基本原理
初始化互斥锁:pthread_mutex_init
获取互斥锁: pthread_mutex_lock
释放互斥锁: pthread_mutex_unlock
销毁互斥锁:pthread_mutex_destroy
互斥锁基本使用代码
死锁示例代码
互斥锁基本原理
在多线程编程中,互斥锁(Mutex,即 Mutual Exclusion 的缩写)是一种最基本的同步机制,用于保证在同一时刻只有一个线程能够访问共享资源,从而避免竞态条件(race condition)的发生。竞态条件指的是多个线程同时访问和修改共享资源,导致最终结果依赖于线程执行顺序的不确定情况。
互斥锁就像是一把锁,线程在访问共享资源前需要先获取这把锁。如果锁是可用的(即未被其他线程持有),线程获取锁并访问共享资源;如果锁已被其他线程持有,那么该线程会被阻塞,直到持有锁的线程释放锁,它才有机会获取锁并继续执行。
大家可以先看一下这篇文章了解一下多线程下信号量的使用:linux多线程之POSIX信号量-CSDN博客。其实互斥锁就跟信号量的值为1的情况下差不多。
初始化互斥锁:pthread_mutex_init
- 函数原型:
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
- 功能:初始化一个互斥锁。
- 参数:
mutex
:指向要初始化的互斥锁变量的指针。attr
:用于指定互斥锁的属性,如设置为NULL
,则使用默认属性。默认属性下,互斥锁是快速互斥锁,适用于大多数场景。如果需要自定义互斥锁属性,如设置为递归互斥锁(允许同一线程多次获取锁),可以先初始化一个pthread_mutexattr_t
结构体,通过相关函数设置属性后再传递给pthread_mutex_init
。
- 返回值:成功时返回 0,失败时返回非零错误码,如
EAGAIN
(资源不足,无法初始化互斥锁)、EINVAL
(无效的属性)等。
获取互斥锁: pthread_mutex_lock
- 函数原型:
int pthread_mutex_lock(pthread_mutex_t *mutex);
- 功能:尝试获取指定的互斥锁。如果互斥锁当前未被其他线程持有,调用线程将获取锁并继续执行;如果互斥锁已被其他线程持有,调用线程会被阻塞,直到互斥锁可用。
- 参数:
mutex
指向要获取的互斥锁变量的指针。 - 返回值:成功时返回 0,失败时返回非零错误码,如
EINVAL
(无效的互斥锁)、EDEADLK
(检测到死锁,例如线程尝试获取自己已经持有的非递归互斥锁)等。
释放互斥锁: pthread_mutex_unlock
- 函数原型:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
- 功能:释放指定的互斥锁,使其他等待该互斥锁的线程有机会获取锁并继续执行。
- 参数:
mutex
指向要释放的互斥锁变量的指针。 - 返回值:成功时返回 0,失败时返回非零错误码,如
EINVAL
(无效的互斥锁)、EPERM
(调用线程未持有该互斥锁却尝试释放它)等。
销毁互斥锁:pthread_mutex_destroy
- 函数原型:
int pthread_mutex_destroy(pthread_mutex_t *mutex);
- 功能:销毁一个已初始化的互斥锁,释放与之相关的资源。在销毁互斥锁之前,应确保没有线程持有该互斥锁。
- 参数:
mutex
指向要销毁的互斥锁变量的指针。 - 返回值:成功时返回 0,失败时返回非零错误码,如
EBUSY
(互斥锁正在被使用)、EINVAL
(无效的互斥锁)等。
互斥锁基本使用代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>// 定义共享资源和互斥锁
int shared_resource = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;// 线程函数
void* increment_shared_resource(void* arg) {// 获取互斥锁pthread_mutex_lock(&mutex);shared_resource++;printf("Thread incremented shared_resource to %d\n", shared_resource);// 释放互斥锁pthread_mutex_unlock(&mutex);return NULL;
}int main() {const int num_threads = 5;pthread_t threads[num_threads];// 创建线程for (int i = 0; i < num_threads; i++) {if (pthread_create(&threads[i], NULL, increment_shared_resource, NULL) != 0) {perror("Thread creation failed");return 1;}}// 等待所有线程完成for (int i = 0; i < num_threads; i++) {if (pthread_join(threads[i], NULL) != 0) {perror("Thread join failed");return 1;}}return 0;
}
死锁示例代码
死锁使得一个或者多个线程被挂起而无法执行,而且这种情况还不容易被发现。在这里咱们简单说明两种会出现死锁的情况:“
- 在一个线程中对一个已经加锁的普通锁再次加锁,将导致死锁。这种情况可能出现在设计得不够仔细的递归函数中。
- 如果两个线程按照不同的顺序来申请两个互斥锁,也容易产生死锁。
我们对第二种情况写一个示例代码:
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>int a = 0;
int b = 0;
pthread_mutex_t mutex_a;
pthread_mutex_t mutex_b;void* another( void* arg )
{pthread_mutex_lock( &mutex_b );printf( "in child thread, got mutex b, waiting for mutex a\n" );sleep( 5 );++b;pthread_mutex_lock( &mutex_a );b += a++;pthread_mutex_unlock( &mutex_a );pthread_mutex_unlock( &mutex_b );pthread_exit( NULL );
}int main()
{pthread_t id;pthread_mutex_init( &mutex_a, NULL );pthread_mutex_init( &mutex_b, NULL );pthread_create( &id, NULL, another, NULL );pthread_mutex_lock( &mutex_a );printf( "in parent thread, got mutex a, waiting for mutex b\n" );sleep( 5 );++a;pthread_mutex_lock( &mutex_b );a += b++;pthread_mutex_unlock( &mutex_b );pthread_mutex_unlock( &mutex_a );pthread_join( id, NULL );pthread_mutex_destroy( &mutex_a );pthread_mutex_destroy( &mutex_b );return 0;
}
代码说明:
- 主线程会优先抢占互斥锁mutex_a,在主线程内我们故意sleep(5)使得程序睡眠五秒钟,此举是为了让新线程抢占互斥锁mutex_b。
- 新线程在主线程sleep(5)结束之前就来到了申请互斥锁mutex_a的代码,此时新线程讲挂起等待互斥锁mutex_a。
- 主线程结束sleep(5)后,会申请互斥锁mutex_b,此时互斥锁mutex_b在新线程手上,主线程也被挂起。
此时两个线程就在那里相互僵着,造成死锁现象。如果我们不是故意让新线程sleep(5)睡眠5秒,该程序很多时候都能正常运行,很难察觉该程序的死锁问题。