gpt4 book ai didi

c - 从多个线程写入一个数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:08 25 4
gpt4 key购买 nike

我正在学习 pthread。

现在我正在尝试编写使用多个pthreads 写入一个二维array 的程序。每个 pthread 只负责 array 的一行。所以那里没有种族或重叠。目标是在不使用全局变量的情况下使其尽可能快。

我实现的第一个解决方案是使用全局变量。它按预期工作。这是代码:

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

int **array;
const int NTHREADS = 5;
const int ELEMENTS = 3;

void *worker(void *arg);
void print_array(int **array);

int main()
{
int i, j;
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));

array = (int**)malloc(sizeof(int*));
for(i = -1; i < NTHREADS; i++)
{
array[i] = (int*)malloc(sizeof(int));
for (j = -1; j < ELEMENTS; j++)
{
array[i][j] = (int)malloc(sizeof(int));
}
}

for (i = 0; i < NTHREADS; i++)
pthread_create(&threads[i], NULL, worker, (void*)i);

for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);

print_array(array);
return 0;
}

void *worker(void *arg)
{
int tid = (int)arg;

for (int j = 0; j < ELEMENTS; j++)
array[tid][j] = j;
return (NULL);
}

void print_array(int **array)
{
for (int i = 0; i < NTHREADS; i++)
{
for (int j = 0; j < ELEMENTS; j++)
printf("%d,", array[i][j]);

printf("\n");
}
}

然后我使用 struct 而不是 global variable 编写了相同的程序。这是代码:

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

const int NTHREADS = 5;
const int ELEMENTS = 3;

typedef struct s_asd
{
int **array;
int tid;
} t_asd;

void *worker(void *arg);
void print_array(int **array);

int main()
{
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
t_asd tmp;
int i, j;

tmp.array = (int**)malloc(sizeof(int*));
for (i = 0; i <= NTHREADS; i++)
{
tmp.array[i] = (int*)malloc(sizeof(int));
for (j = 0; j <= ELEMENTS; j++)
tmp.array[i][j] = (int)malloc(sizeof(int));
}

for (tmp.tid = 0; tmp.tid < NTHREADS; tmp.tid++)
pthread_create(&threads[tmp.tid], NULL, worker, &tmp);

for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);

print_array(tmp.array);
return 0;
}

void *worker(void *arg)
{
t_asd *tmp = (t_asd*)arg;

for (int j = 0; j < ELEMENTS; j++)
tmp->array[tmp->tid][j] = j;
return (NULL);
}

void print_array(int **array)
{
for (int i = 0; i < NTHREADS; i++)
{
for (int j = 0; j < ELEMENTS; j++)
printf("%d,", array[i][j]);

printf("\n");
}
}

这个,打印随机数。我知道我在所有线程中使用相同的指针,但线程本身并没有使用相同的内存区域。那么为什么它打印随机数呢?不使用全局变量的最佳解决方案是什么?

更新 1。第二个程序的输出:

-1413467520,32668,-1413467440,
-1413467584,-1413467568,-1413467552,
-1413467504,-1413467488,-1413467472,
0,1,2,
0,1,2,

最佳答案

尝试这样的事情:

int main()
{
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
t_asd tmp;
int i, j;

tmp.array = (int**)malloc(NTHREADS * sizeof(int*));
for (i = 0; i <= NTHREADS; i++)
{
tmp.array[i] = (int*)malloc(ELEMENTS * sizeof(int));

//can be deleted if you want
for (j = 0; j <= ELEMENTS; j++)
tmp.array[i][j] = 0;
}

for (tmp.tid = 0; tmp.tid < NTHREADS; tmp.tid++) {
t_asd *arg = (t_asd *) malloc(sizeof(t_asd));
memcpy(arg, &tmp, sizeof(t_asd)); //will copy the current tid and the pointer to the array in a new memory area
pthread_create(&threads[tmp.tid], NULL, worker, arg);
}

for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);

print_array(tmp.array);
return 0;
}

当然这是一个例子,你必须释放所有的分配

关于c - 从多个线程写入一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001391/

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