Linux多线程之互斥锁

互斥锁

由于线程共享进程的资源和地址空间,因此在对这些资源进行操作时,必须考虑到线程间资源访问的同步与互斥问题。这里主要介绍 POSIX 中两种线程同步机制,分别为互斥锁和信号量。这两个同步机制可以互相通过调用对方来实现,但互斥锁更适合用于同时可用的资源是惟一的情况;信号量更适合用于同时可用的资源为多个的情况。

互斥锁是用一种简单的加锁方法来控制对共享资源的原子操作。这个互斥锁只有两种状态,也就是上锁和解锁,可以把互斥锁看作某种意义上的全局变量。在同一时刻只能有一个线程掌握某个互斥锁,拥有上锁状态的线程能够对共享资源进行操作。若其他线程希望上锁一个已经被上锁的互斥锁,则该线程就会挂起,直到上锁的线程释放掉互斥锁为止。可以说,这把互斥锁保证让每个线程对共享资源按顺序进行原子操作。

其中,互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。这 3 种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时是否需要阻塞等待。快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止。递归互斥锁能够成功地返回,并且增加调用线程在互斥上加锁的次数,而检错互斥锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误信息。默认属性为快速互斥锁。
互斥锁机制主要包括下面的基本函数

所需头文件 #include <pthread.h>

互斥锁初始化:  pthread_mutex_init()
互斥锁上锁:    pthread_mutex_lock()
互斥锁判断上锁:pthread_mutex_trylock()
互斥锁接锁:    pthread_mutex_unlock()
消除互斥锁:    pthread_mutex_destroy()

pthread_mutex_init()函数
函数原型 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
函数传入值:mutex:互斥锁
          mutexattr:PTHREAD_MUTEX_INITIALIZER:创建快速互斥锁
                    PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:创建递归互斥锁
                    PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:创建检错互斥锁
函数返回值:成功:0 出错:返回错误码

函数原型:
int pthread_mutex_lock(pthread_mutex_t *mutex,)
int pthread_mutex_trylock(pthread_mutex_t *mutex,)
int pthread_mutex_unlock(pthread_mutex_t *mutex,)
int pthread_mutex_destroy(pthread_mutex_t *mutex,)
函数传入值 mutex:互斥锁
函数返回值:成功:0 出错:-1

Linux多线程互斥锁Demo

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define THREAD_NUMBER 3 /* 线程数 */
#define REPEAT_NUMBER 3 /* 每个线程的小任务数 */
#define DELAY_TIME_LEVELS 4.0 /*小任务之间的最大时间间隔*/

#define PTHREAD_MUTEX_EN 0  //是否开启互斥锁


pthread_mutex_t mutex;

void *thrd_func(void *arg)
{
    int thrd_num = (int)arg;
    int delay_time = 0, count = 0;
    int res;

    #if PTHREAD_MUTEX_EN
    /*互斥锁上锁*/
    res=pthread_mutex_lock(&mutex);
    #endif
    do{
        #if PTHREAD_MUTEX_EN
        if(res)
        {
            printf("Thread %d lock failed\n", thrd_num);
            break;
        }
        #endif
        printf("Thread %d is starting\n", thrd_num);

        for(count=0;count<REPEAT_NUMBER;count++)
        {
            delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
            sleep(delay_time);
            printf("\tThread %d: job %d delay = %d\n",thrd_num, count, delay_time);
        }
        printf("Thread %d finished\n", thrd_num);
    }while(0);

    #if PTHREAD_MUTEX_EN
    /* 互斥锁解锁 */
    pthread_mutex_unlock(&mutex);
    #endif
    pthread_exit(NULL);
}

int main()
{
    pthread_t thread[THREAD_NUMBER];
    int no=0,res;
    void *thrd_ret;
    srand(time(NULL));//用时间作为产生随机数的种子

    /*互斥锁初始化*/
    pthread_mutex_init(&mutex,NULL);

    for(no = 0; no < THREAD_NUMBER; no++)
    {
        res=pthread_create(&thread[no],NULL,thrd_func,(void *)no);
        if (res != 0)
        {
            printf("Create thread %d failed\n", no);
            exit(res);
        }
    }
    printf("Create treads success\n Waiting for threads tofinish...\n");

    /*等待线程执结束*/
    for (no = 0; no < THREAD_NUMBER; no++)
    {
        res = pthread_join(thread[no], &thrd_ret);
        if (!res)
        {
            printf("Thread %d joined\n", no);
        }
        else
        {
            printf("Thread %d join failed\n", no);
        }
    }
    pthread_mutex_destroy(&mutex);
    printf("demo exit\n");
    return 0;
}

编译:gcc pthread_mutex.c -o pthread_mutex -lpthread
执行:./pthread_mutex
程序中未使用互斥量时的测试 即PTHREAD_MUTEX_EN 0

./pthread_mutex
Thread 1 is starting
Create treads success
 Waiting for threads tofinish...
Thread 0 is starting
Thread 2 is starting
    Thread 0: job 0 delay = 1
    Thread 1: job 0 delay = 1
    Thread 2: job 0 delay = 2
    Thread 0: job 1 delay = 3
    Thread 1: job 1 delay = 4
    Thread 2: job 1 delay = 4
    Thread 0: job 2 delay = 2
Thread 0 finished
Thread 0 joined
    Thread 2: job 2 delay = 2
Thread 2 finished
    Thread 1: job 2 delay = 4
Thread 1 finished
Thread 1 joined
Thread 2 joined
demo exit

程序中使用互斥量后的测试 即PTHREAD_MUTEX_EN 1
重新编译、执行

第一次:
$ ./pthread_mutex
Create treads success
 Waiting for threads tofinish...
Thread 1 is starting
    Thread 1: job 0 delay = 3
    Thread 1: job 1 delay = 1
    Thread 1: job 2 delay = 4
Thread 1 finished
Thread 0 is starting
    Thread 0: job 0 delay = 3
    Thread 0: job 1 delay = 4
    Thread 0: job 2 delay = 1
Thread 0 finished
Thread 0 joined
Thread 1 joined
Thread 2 is starting
    Thread 2: job 0 delay = 4
    Thread 2: job 1 delay = 2
    Thread 2: job 2 delay = 2
Thread 2 finished
Thread 2 joined
demo exit

第二次:
$ ./pthread_mutex
Create treads success
 Waiting for threads tofinish...
Thread 1 is starting
    Thread 1: job 0 delay = 2
    Thread 1: job 1 delay = 2
    Thread 1: job 2 delay = 1
Thread 1 finished
Thread 2 is starting
    Thread 2: job 0 delay = 3
    Thread 2: job 1 delay = 4
    Thread 2: job 2 delay = 2
Thread 2 finished
Thread 0 is starting
    Thread 0: job 0 delay = 2
    Thread 0: job 1 delay = 1
    Thread 0: job 2 delay = 2
Thread 0 finished
Thread 0 joined
Thread 1 joined
Thread 2 joined
demo exit

第三次:
$ ./pthread_mutex
Create treads success
 Waiting for threads tofinish...
Thread 1 is starting
    Thread 1: job 0 delay = 3
    Thread 1: job 1 delay = 4
    Thread 1: job 2 delay = 3
Thread 1 finished
Thread 0 is starting
    Thread 0: job 0 delay = 3
    Thread 0: job 1 delay = 1
    Thread 0: job 2 delay = 2
Thread 0 finished
Thread 2 is starting
Thread 0 joined
Thread 1 joined
    Thread 2: job 0 delay = 4
    Thread 2: job 1 delay = 2
    Thread 2: job 2 delay = 3
Thread 2 finished
Thread 2 joined
demo exit

通过多次运行不难看出 线程的运行是杂乱无章的,并不一定是誰先创建誰先执行,
加入了互斥锁之后可以看到,线程按照谁先获得锁谁先执行,没得到锁的排队等待的方式按次序运行。

展开阅读全文

没有更多推荐了,返回首页