• 互斥锁,条件变量,信号量的三个小demo


    仨demo

    一、 一个线程读文件,另一个线程将读取的内容输出到终端

    1.1 要求

    • 创建两个线程,
    • 其中一个线程读取文件中的数据,
    • 另外一个线程将读取到的内容打印到终端上,
    • 类似实现cat一个文件。
      • cat数据完毕后,要结束两个线程。
    • 提示:先读数据,读到数据后将数据打印到终端上。

    1.2 代码实现

    /*
    创建两个线程,
    其中一个线程读取文件中的数据,
    另外一个线程将读取到的内容打印到终端上,
    类似实现cat一个文件。
        cat数据完毕后,要结束两个线程。
    提示:先读数据,读到数据后将数据打印到终端上。
    */
    #include 
    
    //  用于暂存的存储
    char buff[16];
    //  用于接收返回值,读取的字节数
    ssize_t res;
    
    //  创建互斥锁
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    //  创建条件变量
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    int flag = 0;
    
    //  读文件
    void *read_file(void *);
    
    //  写到终端
    void *write_out(void *);
    
    int main(int argc, const char *argv[])
    {
        //  以只读的方式打开 “abc.c” 文件
        //  可以换,也可以换成外部参数,按自己需求
        int fd = open("abc.c", O_RDONLY);
    
        //  创建线程1,用于读文件,并将文件描述符传过去
        pthread_t tid1;
        if (pthread_create(&tid1, NULL, read_file, &fd) != 0)
        {
            fprintf(stderr, "pthread_create tid1 error\n");
            return -1;
        }
    
        //  创建线程2,用于写到终端
        pthread_t tid2;
        if (pthread_create(&tid2, NULL, write_out, NULL) != 0)
        {
            fprintf(stderr, "pthread_create tid2 error\n");
            return -1;
        }
    
        //  等待两个线程结束
        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
    
        return 0;
    }
    
    //  读文件
    void *read_file(void *arg)
    {
        //  接收传进来的文件描述符
        int fd = *(int *)arg;
        while (1)
        {
            //  上锁
            pthread_mutex_lock(&mutex);
            //  若标志不是0,说明不该当前线程执行,所以要沉睡
            if (0 != flag)
            {
                //  不是当前线程的执行时机
                pthread_cond_wait(&cond, &mutex);
            }
            //  清空暂存数据的存储空间
            bzero(buff, sizeof(buff));
            //  读取文件中的数据放到暂存空间
            res = read(fd, buff, sizeof(buff));
            //  读取错误则报错,并退出线程
            if (0 > res)
            {
                ERR_MSG("read error");
                return NULL;
            }
            //  读到最后啥都没读到,那就尝试唤醒另一个,然后解锁结束循环,之后终止线程
            else if (0 == res)
            {
                //  尝试唤醒另一个线程
                pthread_cond_signal(&cond);
                //  解锁
                pthread_mutex_unlock(&mutex);
                break;
            }
            //  修改标志
            flag = 1;
            //  当前部分执行完了,该唤醒另一个了
            pthread_cond_signal(&cond);
            //  解锁
            pthread_mutex_unlock(&mutex);
        }
        //  终止当前线程
        pthread_exit(NULL);
    }
    
    //  写到终端
    void *write_out(void *arg)
    {
        while (1)
        {
            //  上锁
            pthread_mutex_lock(&mutex);
    
            //  看是否属于当前该执行的时机
            if (1 != flag)
            {
                //  不属于当前该执行的时机,那就睡过去吧
                pthread_cond_wait(&cond, &mutex);
            }
            //  没东西可以输出,尝试唤醒另一个,并解锁,然后终止当前线程
            if (0 == res)
            {
                //  尝试唤醒另一个线程
                pthread_cond_signal(&cond);
                //  解锁
                pthread_mutex_unlock(&mutex);
                break;
            }
            //  向终端输出
            write(1, buff, res);
            //  修改标志
            flag = 0;
            //  当前任务执行完了,该让另一个线程启动了
            pthread_cond_signal(&cond);
            //  解锁
            pthread_mutex_unlock(&mutex);
        }
        //  终止当前线程
        pthread_exit(NULL);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    二、 三个线程打印ABC,每个线程打一个字符,且顺序不变

    2.1 要求

    • 有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。
      • 要求打印的结果为ABC。

    2.2 代码实现

    /*
    有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。
        要求打印的结果为ABC。
    */
    #include 
    
    //  互斥锁
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    //  条件变量
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    //  标志:0为A,1位B,2为C
    int flag = 0;
    
    //  线程A
    void *print_A(void *);
    //  线程B
    void *print_B(void *);
    //  线程C
    void *print_C(void *);
    
    int main(int argc, const char *argv[])
    {
        //  创建三个线程
        pthread_t tid1;
        pthread_t tid2;
        pthread_t tid3;
        if (pthread_create(&tid1, NULL, print_A, NULL) != 0)
        {
            fprintf(stderr, "pthread_create A error\n");
            return -1;
        }
        if (pthread_create(&tid2, NULL, print_B, NULL) != 0)
        {
            fprintf(stderr, "pthread_create A error\n");
            return -1;
        }
        if (pthread_create(&tid3, NULL, print_C, NULL) != 0)
        {
            fprintf(stderr, "pthread_create A error\n");
            return -1;
        }
    
        //  等待三个线程结束
        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
        pthread_join(tid3, NULL);
        return 0;
    }
    
    //  线程A
    void *print_A(void *arg)
    {
        while (1)
        {
            //  上锁
            pthread_mutex_lock(&mutex);
            //  判断是否是输出A的时机
            if (flag == 0)
            {
                //  是输出A的时机,输出A,然后将标志改为1准备输出B,之后唤醒其他线程
                printf("A");
                flag = 1;
                //  唤醒其他线程
                pthread_cond_signal(&cond);
            }
            //  不是输出A的时机
            else
            {
                //  直接进入休眠
                pthread_cond_wait(&cond, &mutex);
            }
    
            //  解锁
            pthread_mutex_unlock(&mutex);
        }
        //  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)
        pthread_exit(NULL);
    }
    //  线程B
    void *print_B(void *arg)
    {
        while (1)
        {
            //  上锁
            pthread_mutex_lock(&mutex);
            //  判断是否是输出B的时机
            if (flag == 1)
            {
                //  是输出B的时机,输出B,然后将标志改为2准备输出C,之后唤醒其他线程
                printf("B");
                flag = 2;
                //  唤醒其他线程
                pthread_cond_signal(&cond);
            }
            //  不是输出B的时机
            else
            {
                //  直接进入休眠
                pthread_cond_wait(&cond, &mutex);
            }
    
            //  解锁
            pthread_mutex_unlock(&mutex);
        }
        //  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)
        pthread_exit(NULL);
    }
    //  线程C
    void *print_C(void *arg)
    {
        while (1)
        {
            //  上锁
            pthread_mutex_lock(&mutex);
            //  判断是否是输出C的时机
            if (flag == 2)
            {
                //  是输出C的时机,输出C,然后将标志改为0准备输出A,之后唤醒其他线程
                printf("C\n");
                flag = 0;
                //  唤醒其他线程
                pthread_cond_signal(&cond);
            }
            //  不是输出C的时机
            else
            {
                //  直接进入休眠
                pthread_cond_wait(&cond, &mutex);
            }
            //  解锁
            pthread_mutex_unlock(&mutex);
        }
        //  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)
        pthread_exit(NULL);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    三、 用信号量实现一个线程对字符串逆置,另一线程对字符串输出

    3.1 要求

    • 要求定义一个全局变量 char buf[] = “1234567”,创建两个线程,不考虑退出条件。
    • A线程循环打印buf字符串,
    • B线程循环倒置buf字符串,
    • 即buf中本来存储1234567,倒置后buf中存储7654321.
      • B线程中不打印!!
      • 倒置不允许使用辅助数组。
      • 要求A线程打印出来的结果只能为 1234567 或者 7654321
      • 不允许出现7634521 7234567
      • 不允许使用sleep函数
      • 用信号量的方式实现上述代码顺序执行,不允许使用flag;

    3.2 代码实现

    /*
    要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。
    A线程循环打印buf字符串,
    B线程循环倒置buf字符串,
    即buf中本来存储1234567,倒置后buf中存储7654321.
        B线程中不打印!!
        倒置不允许使用辅助数组。
        要求A线程打印出来的结果只能为 1234567  或者  7654321
        不允许出现7634521  7234567
        不允许使用sleep函数
        分析出现错误的原因。
        用信号量的方式实现上述代码顺序执行,不允许使用flag;
    */
    #include 
    
    //  俩信号量
    sem_t sem1;
    sem_t sem2;
    
    //  数据
    char buff[] = "1234567";
    
    //  输出线程
    void *print_str(void *arg);
    //  逆置线程
    void *turn_str(void *arg);
    
    int main(int argc, const char *argv[])
    {
        /*
        这俩信号量第一个初始化为1,第二个初始化为0
        能够保证是先进行线程1,即输出线程,后进行线程2,即逆置线程
        而在输出线程中
            先获取信号量1,操作执行完再释放信号量2,(注意是释放信号量2)
            因为信号量1的初始值为1,所以只会执行一次线程1
            信号量2的初始值为0,在线程1中释放信号量2后变成了1,这样可以执行一次线程2
        在逆置线程中也是同理
            先获取线程1中释放的信号量2,操作执行完再释放信号量1
            获取线程1释放的信号量2,而信号量2只够线程2执行一次,
            在线程2操作执行完后释放信号量1,可以让线程1能够再次执行1次,
            如此往复
        */
        //  信号量初始化为1,也就是在这个信号量下初始只能走一个线程
        if (sem_init(&sem1, 0, 1) < 0)
        {
            ERR_MSG("sem_init");
            return -1;
        }
        //  信号量初始化为0,也就是初始的时候没有可用的信号量
        if (sem_init(&sem2, 0, 0) < 0)
        {
            ERR_MSG("sem_init");
            return -1;
        }
    
        //  创建俩线程
        pthread_t tid1;
        pthread_t tid2;
        if (pthread_create(&tid1, NULL, print_str, NULL) != 0)
        {
            fprintf(stderr, "pthread_create tid1 error\n");
            return -1;
        }
        if (pthread_create(&tid2, NULL, turn_str, NULL) != 0)
        {
            fprintf(stderr, "pthread_create tid2 error\n");
            return -1;
        }
    
        //  等待两个线程结束
        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
        return 0;
    }
    
    //  输出线程
    void *print_str(void *arg)
    {
        while (1)
        {
            //  P操作
            if (sem_wait(&sem1) < 0)
            {
                ERR_MSG("print_str sem_wait");
                return NULL;
            }
            //  要执行的操作
            printf("%s\n", buff);
    
            //  V操作
            if (sem_post(&sem2) < 0)
            {
                ERR_MSG("print_str sem_post");
                return NULL;
            }
        }
        //  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)
        pthread_exit(NULL);
    }
    //  逆置线程
    void *turn_str(void *arg)
    {
        int len = strlen(buff);
    
        while (1)
        {
            //  P操作
            if (sem_wait(&sem2) < 0)
            {
                ERR_MSG("print_str sem_wait");
                return NULL;
            }
    
            //  要执行的操作
            char *s = buff, *s1 = buff + len - 1;
            //  翻转
            while (s < s1)
            {
                (*s) ^= (*s1);
                (*s1) ^= (*s);
                (*s) ^= (*s1);
                s++;
                s1--;
            }
    
            //  V操作
            if (sem_post(&sem1) < 0)
            {
                ERR_MSG("print_str sem_post");
                return NULL;
            }
        }
        //  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)
        pthread_exit(NULL);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
  • 相关阅读:
    AWS安全性身份和合规性之IAM Identity Center(AWS Single Sign-On)
    GPT-SoVITS开源音色克隆框架的训练与调试
    实现Runnable接口
    java计算机毕业设计权益会员管理源码+系统+mysql数据库+lw文档+部署
    OJ练习第173题——单词接龙 II
    51单片机-蜂鸣器
    Java -- 每日一问:Java并发包提供了哪些并发工具类?
    强化学习实践(一)Gym介绍
    【深度学习】《机器学习硕博生建议掌握的九个工具》学习笔记
    service 详解
  • 原文地址:https://blog.csdn.net/qq_52625576/article/details/132841680