#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);
}
views:
391answers:
1
A:
What I learned from the comments is that the pthread_t
structure isn't required to be 'alive' for the duration of thread (which is what I thought and why I used malloc); a stack variable is fine. What I ended up doing was based on Jason Coco's comment:
#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);
}
Jamie
2009-05-15 14:06:19