我正在学习pthreads。
现在我正在尝试使用多个array
创建写入一个2d pthreads
的程序。每个pthread
只负责array
的一行。所以那里没有种族或重叠。
目标是在不使用全局变量的情况下尽可能快地实现它。
我实施的第一个解决方案是使用global variable
的解决方案。它按预期工作。这是代码:
#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,
答案 0 :(得分:1)
您将局部变量tmp作为参数传递给线程并同时在循环中更改它。这是一场数据竞赛,您的线程很可能会在相同的数据上运行。
将tmp转换为数组,填充并将相应的元素传递给相应的线程。
答案 1 :(得分:1)
尝试类似的东西:
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;
}
当然这是一个例子,您必须释放所有分配