C pthread_join根据随机全局变量值挂起
我使用pthreads构建了这段代码。目标是构建一个数组C pthread_join根据随机全局变量值挂起,c,pthreads,freeze,pthread-join,C,Pthreads,Freeze,Pthread Join,我使用pthreads构建了这段代码。目标是构建一个数组X[N][D],并为其分配随机值。您可以将此数组的元素读取为某些点的系数 在下一步中,我将尝试计算一个数组distance[N],该数组保存最后一个元素(Nth)与其他元素之间的所有距离。使用pthreads执行距离计算 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <math.h> #define
X[N][D]
,并为其分配随机值。您可以将此数组的元素读取为某些点的系数
在下一步中,我将尝试计算一个数组distance[N]
,该数组保存最后一个元素(N
th)与其他元素之间的所有距离。使用pthreads
执行距离计算
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#define N 10
#define D 2 //works for any d
#define NUM_THREADS 8
//double *distances;
//int global_index = 0;
pthread_mutex_t lock;
double *X;
typedef struct
{
//int thread_id;
double *distances;
int *global_index ;
pthread_mutex_t lock;
double *X;
}parms;
void *threadDistance(void *arg)
{
parms *data = (parms *) arg;
double *distances = data->distances;
double *X = data->X;
int *global_idx = data -> global_index;
int idx,j;
//long id = (long)arg;
pthread_mutex_lock(&lock);
while(*global_idx<N)
{
//printf("Thread #%ld , is calculating\n", id);
idx = *(global_idx);
(*global_idx)++;
pthread_mutex_unlock(&lock);
for(j=0 ; j<D; j++)
{
distances[idx] = pow(X[(j+1)*N-1]-X[j*N+idx], 2);
//printf("dis[%d]= ", dis);
//printf("%f\n",distances[idx]);
}
//printf("global : %d\n", *global_idx);
}
pthread_exit(NULL);
}
void calcDistance(double * X, int n, int d)
{
int i;
int temp=0;
pthread_t threads[NUM_THREADS];
double *distances = malloc(n * sizeof(double));
parms arg;
arg.X = X;
arg.distances = distances;
arg.global_index = &temp;
for (i=0 ; i<NUM_THREADS ; i++)
{
pthread_create(&threads[i], NULL, threadDistance, (void *) &arg);
}
for(i = 0 ; i<NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
/*----print dstances[] array-------*/
printf("--------\n");
for(int i = 0; i<N; i++)
{
printf("%f\n", distances[i]);
}
/*------------*/
free(distances);
}
int main()
{
srand(time(NULL));
//allocate the proper space for X
X = malloc(D*N*(sizeof(double)));
//fill X with numbers in space (0,1)
for(int i = 0 ; i<N ; i++)
{
for(int j=0; j<D; j++)
{
X[i+j*N] = (double) (rand() / (RAND_MAX + 2.0));
}
}
calcDistance(X, N, D);
return 0;
}
#包括
#包括
#包括
#包括
#定义n10
#定义D 2//适用于任何D
#定义NUM_线程8
//双倍*距离;
//int全局_指数=0;
pthread_mutex_t lock;
双*X;
类型定义结构
{
//int-thread_-id;
双倍*距离;
int*全局指数;
pthread_mutex_t lock;
双*X;
}帕尔姆斯;
void*螺纹距离(void*arg)
{
参数*数据=(参数*)参数;
双*距离=数据->距离;
双*X=数据->X;
int*global\u idx=data->global\u index;
int-idx,j;
//长id=(长)参数;
pthread_mutex_lock(&lock);
而(*global_idx关于:
pthread_mutex_lock(&lock);
while(*global_idx<N)
{
// ...
pthread_mutex_unlock(&lock);
* First of all I cannot understand why the hang depends on the value of N.*
似乎程序实际上是由于seg故障事件而崩溃的,而不是挂起的您的互斥锁有一些问题,例如在while(*global_idxfree(distance);
和printf(“%f\n”,distance[i])内;
-您正在访问释放的内存。@Ingoleonhard谢谢。这就是问题所在。仍然不明白为什么在不同的N值上它没有挂起。在函数中:calcdistance()
前两个循环使用名为i
的变量的实例,该变量是函数的局部变量,然后第三个循环使用循环体内部名为i
的变量。此“阴影”原始变量i
建议每个循环定义一个变量i
变量的范围。在struct
内部和文件范围内声明pthread\u mutex\u t
是一个问题,建议删除结构中的声明。此外,应该初始化互斥变量,类似于:pthread\u mutex\u t lock=pthread\u mutex\u INITIALIZER;