Linux 线程同步之一:互斥锁

    技术2022-05-19  24

    在单线程条件下,由于对数据操作,在同样的时间下,只有一个线程来操作。所以不用担心数据的同步问题。现代的操作系统,大都提供并发机制,虽然有时候是表面的并发。在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的值为最后线程修改过的值。

    #include<stdio.h> #include<pthread.h> void fun_thread1(char * msg); void fun_thread2(char * msg); int g_value = 1; pthread_mutex_t mutex; /*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.*/ int main(int argc, char * argv[]) { pthread_t thread1; pthread_t thread2; if(pthread_mutex_init(&mutex,NULL) != 0 ) { printf("Init metux error."); exit(1); } if(pthread_create(&thread1,NULL,(void *)fun_thread1,NULL) != 0) { printf("Init thread1 error."); exit(1); } if(pthread_create(&thread2,NULL,(void *)fun_thread2,NULL) != 0) { printf("Init thread2 error."); exit(1); } sleep(1); printf("I am main thread, g_vlaue is %d./n",g_value); return 0; } void fun_thread1(char * msg) { int val; val = pthread_mutex_lock(&mutex);/*lock the mutex*/ if(val != 0) { printf("lock error."); } g_value = 0;/*reset the g_value to 0.after that add it to 5.*/ printf("thread 1 locked,init the g_value to 0, and add 5./n"); g_value += 5; printf("the g_value is %d./n",g_value); pthread_mutex_unlock(&mutex);/*unlock the mutex*/ printf("thread 1 unlocked./n"); } void fun_thread2(char * msg) { int val; val = pthread_mutex_lock(&mutex);/*lock the mutex*/ if(val != 0) { printf("lock error."); } g_value = 0;/*reset the g_value to 0.after that add it to 6.*/ printf("thread 2 locked,init the g_value to 0, and add 6./n"); g_value += 6; printf("the g_value is %d./n",g_value); pthread_mutex_unlock(&mutex);/*unlock the mutex*/ printf("thread 2 unlocked./n"); }

    ubuntu10.4Thread 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_lockpthread_mutex_unlock之间的指令。


    最新回复(0)