在C中重新连接线程

在C中重新连接线程,c,pthreads,C,Pthreads,在我的主函数中,我生成了j个线程,它们都并行地计算相同的任务——然后我想等待它们完成,然后退出 int main(...) { // ... int threads = 6; pthread_t* thread = malloc(sizeof(pthread_t)*threads); for(i = 0; i < threads; i++) { struct thread_param *tp; tp = malloc(

在我的主函数中,我生成了j个线程,它们都并行地计算相同的任务——然后我想等待它们完成,然后退出

int main(...) {

    // ...

    int threads = 6;
    pthread_t* thread = malloc(sizeof(pthread_t)*threads);

    for(i = 0; i < threads; i++) {

        struct thread_param *tp;
        tp = malloc(sizeof(*tp));

        // ... 

        int ret = pthread_create(&thread[i], NULL, &control, (void*)tp);

        if(ret != 0) {
            printf ("Create pthread error!\n");
            exit (1);
        }
    }

    for (j = 0; j < threads; j++) {
        printf("JOINING THREAD: %i\n", j);
        pthread_join( &thread[j], NULL);
    }        

    exit(0);
}
int main(…){
// ...
int线程=6;
pthread_t*thread=malloc(sizeof(pthread_t)*threads);
对于(i=0;i

然而,没有什么等待。Main只是在没有完成线程化任务的情况下退出。我遗漏了什么吗?

嘿,试试
pthread\u-join(thread[j],NULL)我认为问题在于类型。我检查了文件:

int pthread\u join(pthread\u t thread,void**value\u ptr)

线程
p\u线程*
线程[j]
p\u线程
,而
线程[j]
p\u线程*
,这是无效的。可能会发生内部错误


编辑:是的,我非常肯定,
pthread\u t
基本上是
int
,所以
pthread\u t*
被接受,它只是无效的线程句柄,所以
pthread\u join
内部失败。

因为您没有向我们展示函数
control()
,很难知道它是否会立即返回-因此不需要等待-或者是否有其他问题。线程函数名前面不需要运算符的地址,这样看起来很好。你所说的“无等待”是什么意思?如果没有&I似乎会收到
程序接收到的信号:“EXC\u BAD\u ACCESS”。
错误。它不会立即返回——它执行一些FTP客户端交互,然后返回。例如,在控制范围内的任何打印语句都不会执行。他们也没有带着我发给他们的文件返回,@Andrey和@paxdiablo都发现了加入问题。我想我只是不明白为什么现在会出现segfault。我想用一个立即返回的函数替换
控件
,看看这是否可以避免segfault。如果是这样的话,那么我会更仔细地研究
控件
。你是对的。这确实让一切都等待,但出于某种原因,当它到达该行时,它会给我一个空指针异常。