[asterisk-dev] Newbie question about ast_pthread_create_background
Alex Massover
alex at jajah.com
Fri Sep 11 05:20:21 CDT 2009
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
-----Original Message-----
From: asterisk-dev-bounces at lists.digium.com [mailto:asterisk-dev-bounces at lists.digium.com] On Behalf Of Russell Bryant
Sent: Thursday, September 10, 2009 6:20 PM
To: Asterisk Developers Mailing List
Subject: Re: [asterisk-dev] Newbie question about ast_pthread_create_background
Alex Massover wrote:
> 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().
The problem is that your arguments to the thread are on the stack. You
need to allocate memory on the heap with ast_malloc(), ast_calloc(), etc.
Also, you probably don't want to use create_background(). You likely
want one of the detached thread variants.
--
Russell Bryant
Digium, Inc. | Engineering Manager, Open Source Software
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at: www.digium.com & www.asterisk.org
_______________________________________________
--Bandwidth and Colocation Provided by http://www.api-digital.com--
AstriCon 2009 - October 13 - 15 Phoenix, Arizona
Register Now: http://www.astricon.net
asterisk-dev mailing list
To UNSUBSCRIBE or update options visit:
http://lists.digium.com/mailman/listinfo/asterisk-dev
This mail was received via Mail-SeCure System.
This mail was sent via Mail-SeCure System.
More information about the asterisk-dev
mailing list