本文介绍了何时使用了pthread_exit(),当在Linux中使用在pthread_join()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的pthreads,我试图去了解它。我看到类似下面的一些例子。

我可以看到的main()通过API阻塞了pthread_exit(),我已经看到了其中,主要功能是通过API受阻的例子在pthread_join()。我无法了解什么时候使用什么?

我指的是以下站点 - https://computing.llnl.gov/tutorials/pthreads/.我不能够得到的时候使用在pthread_join概念(),何时使用了pthread_exit()

能否有人请解释一下吗?此外,一个很好的教程链接pthread的将是AP preciated。

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#定义NUM_THREADS 5无效* PrintHello(void *的主题ID)
{
   长TID;
   TID =(长)主题ID;
   的printf(你好!世界是我,线#%LD \\ n!,TID);
   了pthread_exit(NULL);
}INT主(INT ARGC,CHAR *的argv [])
{
   的pthread_t线程[NUM_THREADS];
   INT RC;
   长吨;
   对于(t = 0; T< NUM_THREADS;吨++){
      的printf(在主:创建线程%LD \\ n,T);
      RC =在pthread_create(安培;螺纹[T],NULL,PrintHello,(无效*)T);
      如果(RC){
         的printf(ERROR;从pthread_create的收益code()为%d \\ n,RC);
         出口(-1);
      }
   }   / *最后一件事是主要的()应该做的* /
   了pthread_exit(NULL);


解决方案

由于在openpub单证解释,

<$c$c>pthread_exit()将退出调用它的线程。

在您的情况下,由于主的话来说,主要的线程的将终止,而你产生的线程将继续执行。这主要是在使用的情况下在哪里
主线程只需要生成线程离开线程做他们的工作。

<$c$c>pthread_join
将暂停已调用它,除非目标线程终止线程的执行

当你想等待线程/ s的前进一步,终止这是有用的情况下
处理主线程。

I am new to pthreads, and I am trying to understand it. I saw some examples like the following.

I could see that the main() is blocked by the API pthread_exit(), and I have seen examples where the main function is blocked by the API pthread_join(). I am not able to understand when to use what?

I am referring to the following site - https://computing.llnl.gov/tutorials/pthreads/. I am not able to get the concept of when to use pthread_join() and when to use pthread_exit().

Can somebody please explain? Also, a good tutorial link for pthreads will be appreciated.

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
解决方案

As explained in the openpub documentations,

pthread_exit() will exit the thread that calls it.

In your case since the main calls it, main thread will terminate whereas your spawned threads will continue to execute. This is mostly used in cases where themain thread is only required to spawn threads and leave the threads to do their job

pthread_joinwill suspend execution of the thread that has called it unless the target thread terminates

This is useful in cases when you want to wait for thread/s to terminate before furtherprocessing in main thread.

A good link for basics

这篇关于何时使用了pthread_exit(),当在Linux中使用在pthread_join()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:28