这是传递给线程声明的结构:
typedef struct {
int rowsPerThread;
int StartingRow;
double co[WIDTH][HEIGHT][2];
uint64_t total_iters;
} thdata;下面是我使用它的方法:(注意malloc)
/* data passed to each thread */
thdata *data[threads_num];
/* create threads */
for (i=0; i<threads_num; i++)
{
data[i]=(thdata *)malloc(sizeof(thdata));
data[i]->rowsPerThread= rowsPerThread;
data[i]->StartingRow= i*rowsPerThread;
for (i=i*rowsPerThread; i<rowsPerThread; i++)
memcpy(data[i]->co[i], Coordinates[i], sizeof (Coordinates) * HEIGHT * 2);
pthread_create(&thread[i], NULL, (void *) &threaded_calc, (void *) &data[i]);
free(data[i]);
}我认为malloc()有一个问题。
这给了我分割的错误。
发布于 2014-09-16 09:32:57
问题是,在创建了一个free之后,您在for块中直接释放了for,而且由于您无法知道thread何时启动,所以可能在thread被scheduler有效启动之前释放了data[i]。
因此,您应该在线程体中调用free。
https://stackoverflow.com/questions/25865106
复制相似问题