简体   繁体   English

在pthread_join()中阻塞

[英]Blocking in pthread_join()

According to the manual page: 根据手册页:

The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated. 除非目标线程已经终止,否则pthread_join()函数应暂停执行调用线程,直到目标线程终止。

So, as I understand, the calling process will block until the specified thread exit. 所以,据我所知,调用进程将阻塞,直到指定的线程退出。

Now consider the following code: 现在考虑以下代码:

pthread_t thrs[NUMTHREADS];

for (int i = 0; i < NUMTHREADS; i++)
{
    pthread_create(&thrs[i], NULL, thread_main, NULL);
}

pthread_join(thrs[0], NULL); /* will be blocked here */
pthread_join(thrs[1], NULL);
pthread_join(thrs[2], NULL);
/* ... */
pthread_join(thrs[NUMTHREADS - 1], NULL);

The calling thread will be blocked in the call to pthread_join(thrs[0], NULL) , until thrs[0] exit in some way. 调用线程将在调用pthread_join(thrs[0], NULL)时被阻塞,直到thrs[0]以某种方式退出。 But how if another thread, for example, thrs[2] call pthread_exit() while we are blocked in the call to pthread_join(thrs[0], NULL) ? 但是,如果另一个线程,例如, thrs[2]调用pthread_exit()而我们在调用pthread_join(thrs[0], NULL)时被阻塞了? Do we have to wait for thrs[0] to exit in order to receive the return value of thrs[2] ? 我们是否必须等待thrs[0]退出才能收到thrs[2]的返回值?

But how if another thread, say thrs[2] exit while we are blocked in the call to pthread_join(thrs[0], NULL)? 但是,如果另一个线程,如果我们在调用pthread_join(thrs [0],NULL)时被阻止,则说[2]退出?

Yes, it could happen. 是的,它可能会发生。 In that case, pthread_join(thrs[2], NULL); 在那种情况下, pthread_join(thrs[2], NULL); will return immediately. 将立即返回。

Do we have to wait for thrs[0] to exit in order to receive the return value of thrs[2]? 我们是否必须等待[0]退出才能收到thrs [2]的返回值?

Yes, you have to wait for thr[0] to terminate. 是的,您必须等待thr[0]终止。


(Not directly related to the question) (与问题没有直接关系)

It's not necessary to call pthread_join() on every thread you create. 没有必要在您创建的每个线程上调用pthread_join() It's a convenient function to get the return status from thread(s). 从线程获取返回状态是一个方便的功能。 If you don't need to know the termination status of the thread, you could create the thread by seeting the "detached" attribute or call pthread_detach(pthread_self()); 如果您不需要知道线程的终止状态,可以通过seread“detached”属性或调用pthread_detach(pthread_self());来创建线程pthread_detach(pthread_self()); from the thread itself to make it detached. 从线程本身使其分离。 In some cases, you would want the threads created to continue execution but no longer need the main thread. 在某些情况下,您希望创建的线程继续执行但不再需要线程。 In that case, you could call pthread_exit(NULL); 在这种情况下,您可以调用pthread_exit(NULL); from main thread which will let other threads to continue even after main thread exits. 从主线程,即使在主线程退出后,其他线程也会继续。

Yes - the main thread blocked on thrs[0] will not get the result from thrs[2] until after thrs[[0] and thrs[1] have also exited. 是的 - 在thrs[0]被阻止的主线程将不会从thrs[2] thrs[0]获得结果,直到thrs[[0]thrs[1]也退出。

If you need more flexibility one option is to do something like having the threads post their results in a queue or signal in some other way that the thread needs to be joined. 如果您需要更多的灵活性,一个选项是做一些事情,比如让线程将结果发布到队列中,或者以线程需要连接的其他方式发出信号。 The main thread can monitor that queue/signal and get the necessary results (which could come from a pthread_join() that is done on the thread that is known to be completed from information int he queue/signal). 主线程可以监视该队列/信号并获得必要的结果(可能来自pthread_join() ,该线程在已知从队列/信号中的信息完成的线程上完成)。

Yes. 是。 The code is executed in serial. 代码是串行执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM