gpt4 book ai didi

c - 在C中的线程之间共享结构

转载 作者:行者123 更新时间:2023-12-03 12:59:09 25 4
gpt4 key购买 nike

我正在做一个程序,其中有2个线程来分析数组。一个(使用者)检查索引是否为1,如果为1,则将其转换为1。另一个线程(生产者)进行相同的操作,但是当找到0时,将其转换为1。

我使用了pthreads,但是我认为每个线程都在创建自己的结构副本,我将它们作为参数传递给了它。
他们应该能够看到另一个线程对该数组所做的更改,但这没有发生。

有人可以告诉我一种使两个线程分析同一数组的方法吗?

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define SPACE 5
#define SEM_GR 1
#define SEM_RE 0

//Declaring a structure to pass the arguments to the threads.
struct arg_struct {
int * semaphore;
int * arr;
};

//Function that prints the array passed as a pointer.
void print_arr (int * a){
for(int i = 0; i < SPACE; i++){
if(i == SPACE - 1){
printf("%d\n", *(a+i));
}
else printf("%d ", *(a+i));
}
}


void * producing (void * arguments){
sleep(2);
printf("Producer started.\n");

//Initializing the arguments structure to use its members.
struct arg_struct *args = arguments;
printf("Producer modifying array in: %x\n", args -> arr);

//Transverse the array.
for (int i = 0; i < SPACE; i++){
sleep(1);

//If the index in the array is empty, and if the semaphore is in green.
printf("Producer checking array...\n");
if(* (args -> arr + i) == 0 && * (args -> semaphore) == SEM_GR){
//Sets the semaphore in red.
* (args -> semaphore) = SEM_RE;
printf("Producing!\n");

//Sets the index in the array as 1.
* (args -> arr + i) = 1;
print_arr(args -> arr);

//Sets the semaphore in green.
* (args -> semaphore) = SEM_GR;

//When the index is the last one in the array, it returns to the first position.
if(i == SPACE - 1){
i = 0;
}
}
}
pthread_exit(NULL);
return NULL;
}

void * consuming (void * arguments){
sleep(2);
printf("Consumer started.\n");

struct arg_struct * args = arguments;
printf("Consumer modifying array in: %x\n", args -> arr);
printf("Consumer checking array...\n");
for (int i = 0; i < SPACE; i++){
sleep(1);
if(* (args -> arr + i) == 1 && * (args -> semaphore) == SEM_GR){
* (args -> semaphore) = SEM_RE;
printf("Consuming!\n");
* (args -> arr + i) = 0;
print_arr(args -> arr);
* (args -> semaphore) = SEM_GR;
if(i == SPACE - 1){
i = 0;
}
}
}
pthread_exit(NULL);
return NULL;
}

int main() {

int * sem;
sem = (int *) malloc(sizeof(int));
* sem = SEM_GR;

int * a;
a = (int *) malloc(SPACE * sizeof(int));

for (int i = 0; i < SPACE; i++) {
a[i] = 1;
}

pthread_t produce, consume;
struct arg_struct args;

args.semaphore = sem;
args.arr = a;

printf("Array address: %x\n", a);
print_arr(a);

pthread_create(&consume, NULL, &consuming, (void *)&args);
pthread_create(&produce, NULL, &producing, (void *)&args);
pthread_join(produce, NULL);
pthread_join(consume, NULL);

return 0;
}

最佳答案

Can someone tell me a way to make that both threads analyze the same array?



您的两个线程似乎都在分析同一数组。但是代码中的 race conditions很少。

为避免争用情况,您需要同步线程。

请注意,从最佳角度来看, a = 5; a = 6;只是 a = 6;。因此,除非您明确告诉编译器 semaphore = RED; semaphore = GREEN;不是普通变量,而是专用的同步原语,否则在代码中 semaphore = GREEN;将被优化为 semaphore

与操作顺序相同。编译器和CPU可能会将 semaphore = RED; arr = 1; semaphore = GREEN;的操作顺序更改为 semaphore = GREEN; arr = 1;,除非您明确告知顺序确实很重要或使用那些也要注意顺序的同步原语。

总体而言,有多种同步方法和库。只需选择一个,尝试修复示例,然后再提出新问题;)

关于c - 在C中的线程之间共享结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48877060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com