小编min*_*oon的帖子

多线程交换2个预定义指针到1个静态指针线程安全吗?

我已经参考了这个链接 - > https://gist.github.com/Taymindis/3938e917aaae4fc480386f494be62f0e并做了一些valgrind检查,它没有错误.但我只想双重确认以下这个例子考虑线程安全吗?

我个人阴道检查,它没有错误,有没有人有更好的主意?

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

#define THREAD_RUN 100

static char *global;

static char *x1 = "This is thread 1";

static char *x2 = "This is thread 2";

void * write(void* thr_data) {
    int n = *(int*)thr_data;

    if(n%2==0) goto RETRY1;
    else goto RETRY2;

RETRY1:
    for (n = 0; n < 1000; ++n) {
        global = x1;
    }
    goto RETRY1;


RETRY2:
    for (n = 0; n < 1000; ++n) {
        global = x2; …
Run Code Online (Sandbox Code Playgroud)

c c++ multithreading assignment-operator

2
推荐指数
1
解决办法
174
查看次数

标签 统计

assignment-operator ×1

c ×1

c++ ×1

multithreading ×1