0
#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);
}
4

1 回答 1

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);
}
于 2009-05-15T14:06:19.293 回答