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);
}
精彩评论