Linux互斥锁的使用代码实现

本文通过两个线程交替增加共享变量的示例,展示了如何使用互斥锁来保护临界区,防止数据竞争。文章提供了注释互斥锁前后程序运行结果的对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From: http://blog.youkuaiyun.com/leo115/article/details/8037869

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <sched.h>  
  4. #include <unistd.h>  
  5.   
  6. //对临界区的保护问题  
  7.   
  8. void *fun1(void *arg);  
  9. void *fun2(void *arg);  
  10.   
  11. int buffer = 0;  
  12. pthread_mutex_t mutex;  
  13. int running = 1;  
  14.   
  15. int main(void )  
  16. {  
  17.     pthread_t pt1;  
  18.     pthread_t pt2;  
  19.   
  20.     pthread_mutex_init(&mutex,NULL);  
  21.   
  22.     pthread_create(&pt1,NULL,fun1,(void*)&running);  
  23.     pthread_create(&pt2,NULL,fun2,(void*)&running);  
  24.   
  25.     usleep(1000);  
  26.     running=0;  
  27.     pthread_join(pt1,NULL);  
  28.     pthread_join(pt2,NULL);  
  29.     pthread_mutex_destroy(&mutex);  
  30.     return 0;  
  31. }  
  32.   
  33. void *fun1(void *arg)  
  34. {  
  35.     while(*(int *)arg)  
  36.     {  
  37.         //pthread_mutex_lock(&mutex);  
  38.     pthread_mutex_lock(&mutex);  
  39.         printf("in fun1 before add , buffer is : %d\n",buffer);  
  40.         usleep(2);  
  41.         buffer++;  
  42.         printf("in fun1 after sleep and add one ,now buffer is %d \n",buffer);  
  43.         //pthread_mutex_unlock(&mutex);  
  44.     pthread_mutex_unlock(&mutex);  
  45.         usleep(2);  
  46.     }  
  47. }  
  48.   
  49. void *fun2(void *arg)  
  50. {  
  51.     while(*(int *)arg)  
  52.     {  
  53.         //pthread_mutex_lock(&mutex);  
  54.     pthread_mutex_lock(&mutex);  
  55.         printf("in fun2 before add , buffer is : %d\n",buffer);  
  56.         usleep(2);  
  57.         buffer++;  
  58.         printf("in fun2 after sleep and add one ,now buffer is %d \n",buffer);  
  59.         //pthread_mutex_unlock(&mutex);  
  60.     pthread_mutex_unlock(&mutex);  
  61.         usleep(2);  
  62.     }  
  63.   
  64. }  

注释互斥锁前的运行结果:


注释互斥锁后的运行结果:




Linux 互斥锁主要是通过内核中的 mutex API 实现的。下面是一个简单的互斥锁示例代码: ``` #include <pthread.h> #include <stdio.h> pthread_mutex_t mutex; void *thread_func(void *arg) { pthread_mutex_lock(&mutex); // 加锁 printf("Thread %d is in critical section\n", *((int*)arg)); pthread_mutex_unlock(&mutex); // 解锁 return NULL; } int main() { pthread_t tid[2]; int thread_num[2] = {1, 2}; pthread_mutex_init(&mutex, NULL); // 初始化互斥锁 pthread_create(&tid[0], NULL, thread_func, &thread_num[0]); pthread_create(&tid[1], NULL, thread_func, &thread_num[1]); pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); pthread_mutex_destroy(&mutex); // 销毁互斥锁 return 0; } ``` 在该示例中,我们首先使用 `pthread_mutex_init()` 初始化互斥锁并创建两个线程。线程函数 `thread_func()` 中,通过 `pthread_mutex_lock()` 和 `pthread_mutex_unlock()` 来进行锁的加解锁操作。 下面是互斥锁的源代码分析: 互斥锁的数据结构定义如下: ``` typedef struct { int count; int owner; struct futex q; } mutex_t; ``` 其中,`count` 表示锁的计数,`owner` 表示当前持有锁的线程 ID,`q` 表示等待队列。 下面是互斥锁的加锁操作 `mutex_lock()` 的源代码: ``` void mutex_lock(mutex_t *lock) { if (atomic_inc_return(&lock->count) == 1) { lock->owner = current_thread_id(); return; } if (lock->owner == current_thread_id()) return; futex_down(&lock->q, lock->count, current_thread_id()); lock->owner = current_thread_id(); } ``` 在该函数中,我们首先通过原子加操作 `atomic_inc_return()` 来将 `lock->count` 加 1,并判断锁是否已经被占用。如果是第一个线程获取锁,那么直接将 `lock->owner` 设置为当前线程 ID 并返回,否则则将当前线程加入到等待队列中并阻塞。 下面是互斥锁的解锁操作 `mutex_unlock()` 的源代码: ``` void mutex_unlock(mutex_t *lock) { if (!atomic_dec_return(&lock->count)) { lock->owner = 0; futex_up(&lock->q, 1); } } ``` 在该函数中,我们首先通过原子减操作 `atomic_dec_return()` 将 `lock->count` 减 1,并判断是否为 0。如果为 0,则将 `lock->owner` 设置为 0 并唤醒等待队列中的一个线程。 综上所述,Linux 互斥锁主要是通过内核中的 mutex API 实现的。在加锁操作中,通过原子操作对计数进行加一,并根据计数判断是否需要将当前线程加入到等待队列中;在解锁操作中,通过原子操作对计数进行减一,并根据计数判断是否需要唤醒等待队列中的一个线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值