[asterisk-dev] Newbie question about ast_pthread_create_background
Alex Massover
alex at jajah.com
Thu Sep 10 10:13:55 CDT 2009
Hi!
I' m trying to build a new application (based on app_skel). The application should get data and send it to the remote server in a background (without blocking the dailplan).
I have a structure with strings:
struct thread_arg {
char *val1;
char *val2;
char *val3;
....
In app_exec(struct ast_channel *chan, void *data):
I populate the struct members by parsing commandline arguments and by calling to pbx_builtin_getvar_helper().
struct thread_arg targs;
targs.val1=(char *)pbx_builtin_getvar_helper(chan, "SIPCALLID");
...
Now I create a detached thread and exit from app_exec():
pthread_t thread;
ast_pthread_create_background(&thread,NULL,&thread_function, &targs);
ast_module_user_remove(u);
return res;
The detached thread sends the structure to the remove server and writes a log with ast_log():
void* thread_function (void* targs)
{
struct thread_arg * pargs = (struct thread_arg *) targs;
ast_log(LOG_ERROR, "in the thread %s \n", pargs->val1);
....
[send to remote server here]
[log result here]
....
The problem is that pargs structure in the thread "destroyed" (contains garbage) from the moment that app_exec() exits and it exits just after creating detached thread. If I put sleep() before exiting from app_exec() everying OK, but the whole idea is to be not blocking. I probaly missing something very basic here.
Please point me to how I can pass the structure to the detached thread and "keep it there" and been able to exit from app_exec().
--
Best Regards,
Alex Massover
This mail was sent via Mail-SeCure System.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-dev/attachments/20090910/3fe690f3/attachment.htm
More information about the asterisk-dev
mailing list