pthread_exit vs. return

У меня есть присоединяемая функция pthread runner, определенная ниже:

void *sumOfProducts(void *param)
{
...
pthread_exit(0);
}

Этот поток должен присоединиться к основному потоку.

Каждый раз, когда я запускал свою программу через Valgrind, я получал следующие утечки :

LEAK SUMMARY:
   definitely lost: 0 bytes in 0 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 968 bytes in 5 blocks
        suppressed: 0 bytes in 0 blocks

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 10)

Я проверил страницу руководства на предмет pthreads, в которой сказано:

  The new thread terminates in one of the following ways:

   * It  calls  pthread_exit(3),  specifying  an exit status value that is
     available  to  another  thread  in  the  same  process   that   calls
     pthread_join(3).

   * It  returns  from  start_routine().   This  is  equivalent to calling
     pthread_exit(3) with the value supplied in the return statement.

   * It is canceled (see pthread_cancel(3)).

   * Any of the threads in the process calls exit(3), or the  main  thread
     performs  a  return  from main().  This causes the termination of all
     threads in the process.

Чудесным образом, когда я заменил pthread_exit () оператором return , утечки исчезли .

return(NULL);

] Мой настоящий вопрос состоит из трех частей:

  1. Может ли кто-нибудь объяснить, почему оператор return не дал утечек?
  2. Есть ли какое-то фундаментальное различие между обоими операторами в отношении выхода из потоков?
  3. Если да, то когда одно должно быть предпочтительнее другого?
37
задан 2 October 2010 в 06:48
поделиться