pthread_t single_thread ;
pthread_create (&single_thread , NULL , &mywriter , 0) ;
void * ignore ;
pthread_join (single_thread , &ignore) ;
我上面有这段代码,定义void * ignore的目的是什么,并且加入它呢? 使用它时出现分段错误。将指针传递给void *到pthread_join的目的是什么?
pthread_t single_thread ;
pthread_create (&single_thread , NULL , &mywriter , 0) ;
void * ignore ;
pthread_join (single_thread , &ignore) ;
我上面有这段代码,定义void * ignore的目的是什么,并且加入它呢? 使用它时出现分段错误。将指针传递给void *到pthread_join的目的是什么?
第二个参数的要点是从线程获取返回的参数。
就像退出代码。但是因为这也可能是你要返回的对象的地址,所以你可以修改void*
。如果你看看签名,它实际上是一个void**
,函数期望你传递给它。
如果你不需要它,因为我认为你的情况。只是通过null
在从一个成功的在pthread_join()返回一个非NULL value_ptr论证,通过了终止线程了pthread_exit()的调用值在由value_ptr引用的位置提供。
以及pthread_join的目的是“将创建的线程”加入到调用者线程中...... 这可以确保您的线程在进程脱离主要的右大括号之前运行。
如:
//code
#include <stdio.h>
#include <pthread.h>
void *func(void *arg)
{
printf ("inside the thread %d\n",pthread_self());
return NULL;
}
int main (void)
{
pthread_t tid;
pthread_create (&tid,NULLm func,NULL);
pthread_join (tid,NULL);
return 0;
}
的pthread_join
的主要目的是同步调用线程与被调用线程。调用线程被挂起,直到被调用的线程退出。
例如,您可以从主函数调用pthread_create
并创建线程。如果不等待线程退出,则主函数/进程将退出,从而将创建的线程连同它一起杀死,而不管它是否完成其工作。
同样如注释中所述,您可以将NULL
作为第二参数传递给pthread_join
。使用未初始化的指针通常会导致分段错误。另外,我很好奇,你怎么能够使用(取消引用)ignore
这是一个void
指针?可能是你铸造的?
这并没有任何意义。只需传递NULL作为第二个参数,而不是未初始化的指针。 – Corbin