#include <pthread.h>
static void * worker_thread(void *);
void some_func(void)
{
pthread_t * tmp;
tmp = malloc(sizeof(pthread_t));
if (NULL != tmp)
{
if (!pthread_create(tmp, NULL, worker_thread, (void *)tmp))
pthread_detach(*tmp);
else
free(tmp);
}
}
static void * worker_thread(void * p)
{
/* do work */
free(p);
return(NULL);
}
答案 0 :(得分:2)
我从评论中学到的是pthread_t
结构在线程持续时间内不需要'活着'(这是我想的以及我使用malloc的原因);堆栈变量很好。我最终做的是基于Jason Coco的评论:
#include <pthread.h>
static void * worker_thread(void *);
void start_worker(void * arg)
{
pthread_t tmp;
(void)pthread_create(& tmp, NULL, worker_thread, arg))
}
static void * worker_thread(void * p)
{
/* do work */
/* finished work */
pthread_detach(pthread_self());
return (p);
}