多线程访问全局变量(两个线程,一读一写)

使用两个线程访问全局字符串,一个线程不停反转字符串,另一个线程读字符串. 

问题: 在反转字符串的过程中,可能另一个线程访问字符串,造成读取错误

解决方案:

使用全局变量控制两个线程执行顺序

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)


char buf[128] = "1234567";
int ready_read = 0;

void reverse(char *buf, int len) {
    char *start = buf;
    char *end = buf + len - 1; 
    while (start < end) {
        char c = *start;
        *start = *end;
        *end = c;
        start ++;
        end --;
    }
}

void *func2(void *arg) {
    while (1) {
        if (ready_read == 0) {
            reverse(buf, strlen(buf));
            ready_read = 1;
        }
    }
}

void *func1(void *arg) {
    while (1) {
        if (ready_read == 1) {
            printf("%s\n", buf);
            ready_read = 0;
        }
    }
}

int main(int argc, const char *argv[]) {
    pthread_t tid1;
    pthread_t tid2;
    if (errno = pthread_create(&tid1, NULL, func1, NULL)) {
        PRINT_ERR("error");
    }
    if (errno = pthread_create(&tid2, NULL, func2, NULL)) {
        PRINT_ERR("error");
    }
    while (1) {
        sleep(1);
    }
    return 0;
}

执行结果:

unbuntu@unbuntu:~ $ ./a.out 
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
^C

加锁防止写过程中被读,或者读过程中被写

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

pthread_mutex_t lock;
char buf[128] = "1234567";

void reverse(char *buf, int len) {
    char *start = buf;
    char *end = buf + len - 1; 
    while (start < end) {
        char c = *start;
        *start = *end;
        *end = c;
        start ++;
        end --;
    }
}

void *func2(void *arg) {
    while (1) {
        pthread_mutex_lock(&lock);
        reverse(buf, strlen(buf));
        pthread_mutex_unlock(&lock);
    }
}

void *func1(void *arg) {
    while (1) {
        pthread_mutex_lock(&lock);
        printf("%s\n", buf);
        pthread_mutex_unlock(&lock);
    }
}

int main(int argc, const char *argv[]) {
    pthread_mutex_init(&lock, NULL);
    pthread_t tid1;
    pthread_t tid2;
    if (errno = pthread_create(&tid1, NULL, func1, NULL)) {
        PRINT_ERR("error");
    }
    if (errno = pthread_create(&tid2, NULL, func2, NULL)) {
        PRINT_ERR("error");
    }
    while (1) {
        sleep(1);
    }
    return 0;
}

执行结果: (此时线程不是顺序执行, 但读到的数据是正常的)

unbuntu@unbuntu:~ $ ./a.out
7654321
7654321
7654321
7654321
7654321
1234567
1234567
1234567
1234567
1234567
1234567
1234567
1234567
1234567
1234567
7654321
7654321
7654321
7654321
^C
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值