Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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++_C_Multithreading - Fatal编程技术网

C++ 运行多线程程序时只有一个线程工作(线程内有线程)

C++ 运行多线程程序时只有一个线程工作(线程内有线程),c++,c,multithreading,C++,C,Multithreading,我在运行多线程应用程序时遇到问题。让我解释一下。我有一个for循环,它创建了n个线程,这n个线程再次为每个线程创建了2个线程。 最后,我有2*n个线程必须同时运行 我可以在我的程序中看到创建了n对线程,但是在创建了n个线程之后,只有第一对线程在工作。最后的n-1个其他两个线程什么也不做 下面是我的代码的样子: void *thread1_send (void * arg_threads) { // do some work pthread_exit (0); } void

我在运行多线程应用程序时遇到问题。让我解释一下。我有一个for循环,它创建了n个线程,这n个线程再次为每个线程创建了2个线程。 最后,我有2*n个线程必须同时运行

我可以在我的程序中看到创建了n对线程,但是在创建了n个线程之后,只有第一对线程在工作。最后的n-1个其他两个线程什么也不做

下面是我的代码的样子:

void *thread1_send (void * arg_threads)
{  
    // do some work

    pthread_exit (0);
}

void *thread2_listen (void * arg_threads)
{
    // do some work

    pthread_exit (0);
}


void *thread_multi (void * arg_threads)
{
    // first thread
    if (pthread_create (&th1, NULL, thread1_send, arg_threads) < 0) {   
        printf ("Error pthread_create for thread 1\n");
        exit (1);
    }

    //second thread
    if (pthread_create (&th2, NULL, thread2_listen, arg_threads) < 0) {   
        printf ("Error pthread_create for thread 2\n");
        exit (1);
    }

    (void)pthread_join (th1, &ret_t);
    (void)pthread_join (th2, &ret_t);

    pthread_exit(0);
} 



pthread_t * th_multi = (pthread_t *) malloc( n * sizeof(*th_multi) );
....
....
// for loop in a function
for(int i=0; i<n; i++)
{                                                                              
    // creation of n threads
    if (pthread_create (&th_multi[i], NULL, thread_multi, arg_threads) < 0) {   
        printf ("Error pthread_create for thread_multi [%d].\n", i);
        exit (1);
    }

    pthread_exit(0); // it's unclear for me what pthread_exit is doing here.

} // end of for
void*thread1\u发送(void*arg\u线程)
{  
//做些工作
pthread_退出(0);
}
void*thread2\u侦听(void*arg\u线程)
{
//做些工作
pthread_退出(0);
}
void*线程\u多线程(void*arg\u线程)
{
//第一根线
如果(pthread_create(&th1,NULL,thread1_send,arg_threads)<0){
printf(“错误pthread_为线程1创建\n”);
出口(1);
}
//第二线
if(pthread_create(&th2,NULL,thread2_listen,arg_threads)<0){
printf(“错误pthread_为线程2创建\n”);
出口(1);
}
(void)pthread_join(th1和ret_t);
(void)pthread_join(th2和ret_t);
pthread_退出(0);
} 
pthread_t*th_multi=(pthread_t*)malloc(n*sizeof(*th_multi));
....
....
//函数中的for循环

对于(int i=0;底部的pthread_exit()可能会消失…主线程是否对其创建的线程执行过pthread_join()?如果没有,则在main()返回后运行的关闭代码可能会在这些线程使用完资源之前清理这些线程正在使用的资源。我没有执行任何pthread_join()对于在循环中创建的线程。如果需要,在哪里需要它们?在循环内还是在循环外?对于pthread_exit(),如果我把它放在循环外,我会将焦点放在创建的最后一个线程上。通常程序必须继续运行,直到我停止它。这意味着线程不需要为我结束。它们在(1)中发送和侦听消息。那么,即使我从未从主线程返回,我真的需要加入它们吗?pthread_join()将阻塞,直到指定的线程退出。放置pthread_join()因此,循环内部会阻止主线程在第一个线程退出之前启动第二个子线程,这可能不是您想要的。因此,在第一个循环之后应该有一个调用pthread_join()的第二个循环如果您的子线程从未退出,那么第二个循环将无限期地阻塞其对pthread_join()的第一次调用,这就足够了——但是如果/当您决定允许程序关闭时,这将允许完全关闭。因此,我删除了pthread_exit()在循环中添加第二个循环,以便按照建议加入,但现在这只是最后一个线程在工作,而n-1的前两个线程什么也不做。那么问题出在哪里呢?从线程1和线程2正在做的工作中?线程优先级/同步?可重入性?很难说。我要做的是将printf的无处不在地散布到find找出每个线程被阻止的位置,或者它是否已退出,或者从未启动过,或者是什么。(您可以执行printf(“%i:在第%s行:%i\n”,(int)pthread\u self(),\uuuu FILE\uuuuuuu,\uuuuuuuuu line\uuuuuuu);以获取显示何时在哪一行执行哪个线程的输出)。