[asterisk-dev] Newbie question about ast_pthread_create_background
Mark Michelson
mmichelson at digium.com
Fri Sep 11 09:57:17 CDT 2009
Alex Massover wrote:
> Hi!
>
> Thank you for spending your time on beginners :)
>
> I'm able to ast_malloc() memory for the thread argument structure, and ast_free() it in the thread. But still have troubles with structure members strings (char *) - it either doesn't pass well or I run into core dumps (memory corruption). But the idea is clear to me now, probably I just don't implement it correctly.
>
> I fixed it differently - with semaphore (no malloc()). I pass a local (stack) structure to the thread and sem_wait() before return. In the thread I copy the data from the stuct to a local variables with ast_strdupa() and sem_post().
> What do you think is it good enough?
>
> On my 1.4 I have only 2 builtins: ast_pthread_create() and ast_pthread_background(). I just pass PTHREAD_CREATE_DETACHED attr to have a detached thread.
>
> --
> Best Regards,
> Alex Massover
If you're going to create a thread and then wait for it to complete before
continuing, why not just call a function instead?
As for the previous problem you had where the individual components of the
malloced structure were garbage, you likely would need to do a deeper copy than
what you had done. In other words, instead of doing something like
my_struct->my_char_ptr = some_auto_char_ptr;
do something like
my_struct->my_char_ptr = ast_strdup(some_auto_char_ptr);
You'd have to make sure to call ast_free on my_struct->my_char_ptr after you are
done with it.
Mark Michelson
More information about the asterisk-dev
mailing list