开发者

Is there a simple way use pthread_t variables?(multiple calls of thread spawning function)

开发者 https://www.devze.com 2023-03-25 06:17 出处:网络
I want to make a function asynchronous (setup function just开发者_Go百科 calls into real function, no return of status) but I don\'t want to complicate things with a thread array and an extra mutex to

I want to make a function asynchronous (setup function just开发者_Go百科 calls into real function, no return of status) but I don't want to complicate things with a thread array and an extra mutex to protect the index.

I tried just allowing a small memory leak by mallocing and loosing a thread variable on the heap but this seems to segfault every other time(can anybody geuss why?).

This is a small simple program(which I would prefer not to complicate any more) and the function is unlikely to be called > 100 times but I'd prefer to avoid leaking memory or extra locks.

*pthread_t is defined to unsigned long in linux and this doesn't have to be portable P.S. is there a way to have free(thread) called at end of thread without blocking?


example of leaking code:

void * real_func(void *t)
{
    pthread_mutex_lock(t->something);
    ...
    pthread_mutex_unlock(t->something)l
}

void async_func(type_a *a, type_a *b)
{
pthread_t * thr = malloc(sizeof(pthread_t));
struct two_type_as t;
two_type_as.a=a;
two_type_as.b=b;

pthread_create(thr, NULL, real_func, &t);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号