【发布时间】:2015-04-26 13:21:37
【问题描述】:
我是线程新手,使用 pthread_join 时遇到问题。
这是我的代码:
int create_threads(t_lemin *lem)
{
int i;
pthread_t **threads;
int j;
void *ret;
t_tree *tmp;
j = -1;
printf("starting algo\n");
threads = xmalloc(sizeof(pthread_t));
i = count_leaf(lem->start);
tmp = lem->start;
printf("%d\n", tmp->visited[0]);
while (++j != i)
{
tmp->leaf[j]->thread_nbr = j;
pthread_create(threads[j], NULL, find_way_out, tmp->leaf[j]);
usleep(100);
}
i = -1;
while (++j != i)
(void)pthread_join (*threads[j], &ret);
//printf("%d\n", *((int*)ret));
return (0);
}
我的一个线程完成了他的工作,在第二个线程完成后,我遇到了分段错误。 我的函数 find_way_out 返回 pthread_exit((void*)j);或 pthread_exit(0); 其中 j 是一个整数指针。 你知道它来自哪里吗?
谢谢!
【问题讨论】:
-
什么是 xmalloc?你在哪里为
threads元素分配内存? -
xmalloc 只是一个验证 malloc 并在 malloc 失败时退出的函数。它返回 void * .
标签: c