Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C程序运行期间添加线程?_C_Multithreading_Pthreads - Fatal编程技术网

如何在C程序运行期间添加线程?

如何在C程序运行期间添加线程?,c,multithreading,pthreads,C,Multithreading,Pthreads,我正在添加用户想要的线程。这意味着我在这里,如果他想要新的线程,我就创建它。用户给线程指定了一个特定的名称,所以我不能比他希望的更快地创建它。 我的部分代码: while(read != EOF) { if(user_wants_new_thread) { worker_t *data = malloc(sizeof(worker_t)); data->name = malloc((strlen(arg1) + 1) * sizeof(char

我正在添加用户想要的线程。这意味着我在这里,如果他想要新的线程,我就创建它。用户给线程指定了一个特定的名称,所以我不能比他希望的更快地创建它。 我的部分代码:

while(read != EOF) {
    if(user_wants_new_thread) {

        worker_t *data = malloc(sizeof(worker_t));

        data->name  = malloc((strlen(arg1) + 1) * sizeof(char));
        strcpy(data->name, arg1);

        pthread_create(&thread, NULL, worker, (void *) data);
        pthread_join(thread, NULL);

        pthread_mutex_lock(&mutex);

        pthread_cond_signal(&cond);

        pthread_mutex_unlock(&mutex);

    }
}
线程应该在不等待的情况下开始工作。现在它等待worker函数的完成。
感谢您的帮助:)

是的,当我使用
pthread\u-join(thread,NULL)时它将等待创建的线程结束。因此有必要删除这一行。

pthread\u create()
之后立即调用
pthread\u join()
几乎肯定是一个错误。如果调用pthread\u join(),您将被卡住,直到线程完成。如果要与线程的启动同步,应使用pthread\u barrier\u init()、pthread\u barrier\u wait()。。。