Linux 线程同步之一:互斥锁

在单线程条件下,由于对数据操作,在同样的时间下,只有一个线程来操作。所以不用担心数据的同步问题。现代的操作系统,大都提供并发机制,虽然有时候是表面的并发。在Linux中,并发用的最多的是基于线程的并发,进程的代价太高了,这样,一个共享的数据,在同一时间内,可能有多个线程在操作。如果没有同步机制,那么想要保证每个线程操作的正确性,是很困难的。

1互斥锁概念:

互斥锁提供一个可以在同一时间,只让一个线程访问临界资源的的操作接口。互斥锁(Mutex)是个提供线程同步的基本锁。让上锁后,其他的线程如果想要锁上,那么会被阻塞,直到锁释放后。

如果,在锁释放后,有多个线程被阻塞,那么,所有的被阻塞的线程会被设为可执行状态。第一个执行的线程,取得锁的控制权,上锁。其他的线程继续阻塞。

2:互斥锁系统原型

互斥锁的系统原型为:pthread_mutex_t,在用互斥锁之前,必须要初始化互斥锁,可以调用pthread_mutex_init;或者是PTHREAD_MUTEX_INITIALZER(仅用于静态分配内存)如果我们动态分配互斥锁(比如,用malloc),那么,在释放内存之前,必须调用pthread_mutex_destroy;

下面为互斥锁初始化和销毁的函数原型:

#include <pthread.h>

int pthread_mutex_init(pthread_mutex_t *restrict mutex,

                   const pthread_mutexattr_t *restrict attr);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

Both return: 0 if OK, error number on failure

互斥锁的上锁和解锁的函数原型为:

#include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

All return: 0 if OK, error number on failure

如果要锁上一个互斥锁用pthread_mutex_lock;解锁用pthread_mutex_unlock;如果,一个线程不想被阻止,那么可以用pthread_mutex_trylock函数来上锁。如果一个线程调用pthread_mutex_trylock时,锁变量没有被其他的线程锁上,那么pthread_mutex_trylock会锁上锁变量,返回值0,表示成功。否则,pthread_mutex_trylock失败,返回EBUSY,没有上锁。

下面为一个使用互斥锁来实现,共享数据同步例子。在程序中有一个全局变量g_value,和互斥锁mutex,在线程1中,重置g_value值为0,然后加5;在线程2中,重置g_value值为0,然后加6;最后在主线程中输出g_value的值,这是g_value的值为最后线程修改过的值。

  1. #include<stdio.h>  
  2. #include<pthread.h>  
  3.   
  4. void  fun_thread1(char * msg);  
  5. void  fun_thread2(char * msg);  
  6. int g_value = 1;  
  7. pthread_mutex_t mutex;  
  8. /*in the thread individual, the thread reset the g_value to 0,and add to 5 int the thread1,add to 6 in the thread2.*/  
  9. int main(int argc, char * argv[])  
  10. {  
  11.     pthread_t thread1;  
  12.     pthread_t thread2;  
  13.     if(pthread_mutex_init(&mutex,NULL) != 0 )  
  14.     {  
  15.         printf("Init metux error.");  
  16.         exit(1);  
  17.     }  
  18.     if(pthread_create(&thread1,NULL,(void *)fun_thread1,NULL) != 0)  
  19.     {  
  20.         printf("Init thread1 error.");  
  21.         exit(1);  
  22.     }  
  23.     if(pthread_create(&thread2,NULL,(void *)fun_thread2,NULL) != 0)  
  24.     {  
  25.         printf("Init thread2 error.");  
  26.         exit(1);  
  27.     }  
  28.     sleep(1);  
  29.     printf("I am main thread, g_vlaue is %d./n",g_value);  
  30.     return 0;  
  31. }  
  32. void  fun_thread1(char * msg)  
  33. {  
  34.     int val;  
  35.     val = pthread_mutex_lock(&mutex);/*lock the mutex*/  
  36.     if(val != 0)  
  37.     {  
  38.         printf("lock error.");  
  39.     }  
  40.     g_value = 0;/*reset the g_value to 0.after that add it to 5.*/  
  41.     printf("thread 1 locked,init the g_value to 0, and add 5./n");  
  42.     g_value += 5;  
  43.     printf("the g_value is %d./n",g_value);  
  44.     pthread_mutex_unlock(&mutex);/*unlock the mutex*/  
  45.     printf("thread 1 unlocked./n");  
  46. }  
  47. void  fun_thread2(char * msg)  
  48. {     
  49.     int val;  
  50.     val = pthread_mutex_lock(&mutex);/*lock the mutex*/  
  51.     if(val != 0)  
  52.     {  
  53.         printf("lock error.");  
  54.     }  
  55.     g_value = 0;/*reset the g_value to 0.after that add it to 6.*/  
  56.     printf("thread 2 locked,init the g_value to 0, and add 6./n");  
  57.     g_value += 6;  
  58.     printf("the g_value is %d./n",g_value);  
  59.     pthread_mutex_unlock(&mutex);/*unlock the mutex*/  
  60.     printf("thread 2 unlocked./n");  
  61. }  

在ubuntu10.4,Thread model: posix,gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)编译,运行如下:

thread 2 locked,init the g_value to 0, and add 6.

the g_value is 6.

thread 2 unlocked.

thread 1 locked,init the g_value to 0, and add 5.

the g_value is 5.

thread 1 unlocked.

I am main thread, g_vlaue is 5.

总结:

关于互斥锁,可能有些地方,不太容易懂。比如,互斥锁锁什么?简单的来说,互斥锁用的限制在同一时刻,其他的线程执行pthread_mutex_lock和pthread_mutex_unlock之间的指令。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值