【Linux多线程编程】互斥锁及其使用

1、互斥锁

用于解决竞争问题的一种机制。

什么是竞争,竞争就是多个实体同时获取一个资源,例如多个线程写一个全局变量。

2、Linux如何使用互斥锁

以pthread为例,锁的创建和使用如下:

/* 创建锁 */
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

/* 加锁*/
pthread_mutex_lock(&lock);

/* 解锁 */
pthread_mutex_unlock(&lock);


3、多写者问题


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <pthread.h>

/* global variable */
int gValue = 0;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *threadFuncReader(void *arg)
{
    while(1)
    {
        gValue = 1;

        if(1 != gValue)
        {
            printf("\nerror");
        }


       // printf("\nthis is [1], read value is [%d]", gValue);


        usleep(1);
    }


}


void *threadFuncWriter(void *arg)
{
    while(1)
    {


        gValue = 2;

        if(2 !=gValue )
        {
            printf("\nerror");

        }
        else
        {
           // printf("\nthis is [2], read value is [%d]", gValue);          
        }


        usleep(1);
    }


}

void *threadFuncWriter1(void *arg)
{
    while(1)
    {

        gValue = 3;

        if(3 != gValue )
        {
            printf("\nerror");

        }
        else
        {
            //printf("\nthis is [3], read value is [%d]", gValue);          
        }


        usleep(1);
    }


}

void *threadFuncWriter2(void *arg)
{
    while(1)
    {

        gValue = 4;

        if(4 != gValue )
        {
            printf("\nerror");
        }
        else
        {
            printf("\nthis is [4], read value is [%d]", gValue);          
        }


        sleep(1);
    }


}

int main(int argc, int **argv)
{


    /* shared resources */


    pthread_t tid_reader;
    pthread_t tid_writer;
    pthread_t tid_writer1;
     pthread_t tid_writer2;

    pthread_create(&tid_reader, NULL, threadFuncReader, NULL);
    pthread_create(&tid_writer, NULL, threadFuncWriter, NULL);
     pthread_create(&tid_writer1, NULL, threadFuncWriter1, NULL);
     pthread_create(&tid_writer2, NULL, threadFuncWriter2, NULL);

    pthread_join(tid_reader, NULL);
    pthread_join(tid_writer, NULL);
    pthread_join(tid_writer1, NULL);
    pthread_join(tid_writer2, NULL);

    printf("\n hello world in Linux.");

    return 0;
}

上述代码由于没有锁,运行结果如下:

this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
error
this is [4], read value is [4]

加锁后的代码:


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <pthread.h>

/* global variable */
int gValue = 0;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *threadFuncReader(void *arg)
{
    while(1)
    {

        pthread_mutex_lock(&lock);
        gValue = 1;

        if(1 != gValue)
        {
            printf("\nerror");
        }


       // printf("\nthis is [1], read value is [%d]", gValue);

        pthread_mutex_unlock(&lock);

        usleep(1);
    }


}


void *threadFuncWriter(void *arg)
{
    while(1)
    {

        pthread_mutex_lock(&lock);

        gValue = 2;

        if(2 !=gValue )
        {
            printf("\nerror");

        }
        else
        {
           // printf("\nthis is [2], read value is [%d]", gValue);          
        }
        pthread_mutex_unlock(&lock);


        usleep(1);
    }


}

void *threadFuncWriter1(void *arg)
{
    while(1)
    {

        pthread_mutex_lock(&lock);
        gValue = 3;

        if(3 != gValue )
        {
            printf("\nerror");

        }
        else
        {
            //printf("\nthis is [3], read value is [%d]", gValue);          
        }

        pthread_mutex_unlock(&lock);

        usleep(1);
    }


}

void *threadFuncWriter2(void *arg)
{
    while(1)
    {

        pthread_mutex_lock(&lock);
        gValue = 4;

        if(4 != gValue )
        {
            printf("\nerror");
        }
        else
        {
            printf("\nthis is [4], read value is [%d]", gValue);          
        }

        pthread_mutex_unlock(&lock);

        sleep(1);
    }


}

int main(int argc, int **argv)
{


    /* shared resources */


    pthread_t tid_reader;
    pthread_t tid_writer;
    pthread_t tid_writer1;
     pthread_t tid_writer2;

    pthread_create(&tid_reader, NULL, threadFuncReader, NULL);
    pthread_create(&tid_writer, NULL, threadFuncWriter, NULL);
     pthread_create(&tid_writer1, NULL, threadFuncWriter1, NULL);
     pthread_create(&tid_writer2, NULL, threadFuncWriter2, NULL);

    pthread_join(tid_reader, NULL);
    pthread_join(tid_writer, NULL);
    pthread_join(tid_writer1, NULL);
    pthread_join(tid_writer2, NULL);

    printf("\n hello world in Linux.");

    return 0;
}

运行结果:

this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]

相关推荐

  1. Linux线编程互斥及其使用

    2024-02-04 05:22:01       29 阅读
  2. 【C++线编程】(一)之详解互斥mutex

    2024-02-04 05:22:01       41 阅读
  3. Unix环境高级编程-学习-07-线互斥

    2024-02-04 05:22:01       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-04 05:22:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-04 05:22:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-04 05:22:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-04 05:22:01       20 阅读

热门阅读

  1. NFS服务搭建

    2024-02-04 05:22:01       30 阅读
  2. 【30秒看懂大数据】变量

    2024-02-04 05:22:01       35 阅读
  3. Unity Best Http插件的基本使用

    2024-02-04 05:22:01       27 阅读
  4. Unity_PackageManager缺失

    2024-02-04 05:22:01       31 阅读
  5. SQL Limit

    2024-02-04 05:22:01       32 阅读
  6. docker镜像变量传递

    2024-02-04 05:22:01       26 阅读
  7. 【Node系列】Buffer详解

    2024-02-04 05:22:01       28 阅读
  8. Python 机器学习 K-近邻算法 鸢尾花种类预测

    2024-02-04 05:22:01       31 阅读
  9. Android PMS——网络下载应用安装(六)

    2024-02-04 05:22:01       27 阅读
  10. MongoDB的索引与索引字段的顺序

    2024-02-04 05:22:01       29 阅读