[asterisk-dev] Accessing a channel from different threads

Tilghman Lesher tilghman at mail.jeffandtilghman.com
Mon Oct 8 11:37:03 CDT 2007


On Sunday 07 October 2007 14:38, Corrado Santoro wrote:
> I'm writing an Asterisk application and, for some reason, I need to
> access the same "ast_channel" from two different threads: one thread
> basically executes "ast_waitfor_nandfds" and the other thread "ast_write".
>
> So I would like to ask if the access to the channel is thread-safe, or I
> need e.g. a mutex to avoid inconsistencies.

You need to lock and hold the chan->lock mutex for any period that you access
and/or change channel structure elements.  Note that if you access more than
one channel lock at a time, you may need to use ast_mutex_trylock() in a loop
to avoid possible deadlocks (since there is no locking order for different
channels in place).

-- 
Tilghman



More information about the asterisk-dev mailing list