[asterisk-dev] Running a thread

MohammedShehzad pmhshz at gmail.com
Wed Jan 6 01:36:10 CST 2010


On Tue, Jan 5, 2010 at 9:27 PM, Russell Bryant <russell at digium.com> wrote:

> On 01/05/2010 12:31 AM, MohammedShehzad wrote:
> > Hello every body,
> >
> > I am facing some problems in some custom changes in rtp.c file in which
> have
> > used some threading functions.
> > I create a thread in ast_rtp_new_with_bindaddr function as below:
> >
> >              pthread_attr_t attr;
> >              rtp->thread_var=NULL;
> >              pthread_attr_init(&attr);
> >              ast_pthread_create_background(&rtp->thread_var,&attr,
> > somecustom_thread,(void *)rtp);
> >              pthread_attr_destroy(&attr);
> >
> > Thread of "somecustom_thread" is being created, which continue in while
> loop
> > (i.e. while(1))
> > I destroy this thread in ast_rtp_stop function with below line:
> > pthread_cancel(rtp->thread_var);
> > *
> > *My function works very fine in normal one-two calls. But when I put the
> > modified asterisk in regression/stress testing (test with many calls of
> > 20-30 second duration for long run), I finds the memory leaks as below
> under
> > Valgrind:
> > ==19707== 98,320 bytes in 683 blocks are possibly lost in loss record 39
> of
> > 41
> > ==19707==    at 0x4004864: calloc (vg_replace_malloc.c:279)
> > ==19707==    by 0xB09109: _dl_allocate_tls (in /lib/ld-2.7.so)
> > ==19707==    by 0xCA9BFE: pthread_create@@GLIBC_2.1 (in /lib/
> > libpthread-2.7.so)
> > ==19707==    by 0x812276E: ast_pthread_create_stack (utils.c:910)
> > ==19707==    by 0x80EE60A: ast_rtp_new_with_bindaddr (rtp.c:2124)
> > ==19707==    by 0x4AD8D0E: sip_alloc (chan_sip.c:4677)
> > ==19707==    by 0x4ADA318: find_call (chan_sip.c:4848)
> > ==19707==    by 0x4B152DA: sipsock_read (chan_sip.c:16485)
> > ==19707==    by 0x80C11B9: ast_io_wait (io.c:279)
> > ==19707==    by 0x4B164D6: do_monitor (chan_sip.c:16772)
> > ==19707==    by 0x8122526: dummy_start (utils.c:856)
> > ==19707==    by 0xCA950A: start_thread (in /lib/libpthread-2.7.so)
> >
> > *
> > *
> > This seems to be a problem with pthread library, but it only occurs which
> I
> > do regression testing.
> > My question is that what is the mistake in creating and destroying the
> > thread.
> > If the there is a mistake in my code of creating the thread, please let
> me
> > know.
> > If any body know better option to run the thread just after RTP media
> starts
> > and stop the thread when RTP media stop, please point me into that
> > direction.
> >
> > Thanks in advance.
>
> You should be creating your thread as detached or be running
> pthread_join() after you stop the thread you have created.
>
> --
> Russell Bryant
> Digium, Inc. | Engineering Manager, Open Source Software
> 445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
> www.digium.com -=- www.asterisk.org -=- blogs.asterisk.org
>
> _______________________________________________
> --Bandwidth and Colocation Provided by http://www.api-digital.com--
>
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>   http://lists.digium.com/mailman/listinfo/asterisk-dev
>

Hi,

Thanks Russell,
I made the thread detachable by replacing ast_pthread_create_background with
below code:
            pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
            ast_pthread_create(&rtp->thread_var, &attr,
ast_rtp_write_thread, rtp);

It resolved the above leak, it seems that there is another leak too as
below, which did not get resolved:

==3112== 1,241,886 bytes in 2,849 blocks are still reachable in loss record
41 of 41
==3112==    at 0x4005525: malloc (vg_replace_malloc.c:149)
==3112==    by 0x8075B0A: __ast_alloc_region (astmm.c:115)
==3112==    by 0x8075AC5: __ast_calloc (astmm.c:198)
==3112==    by 0x8120CFB: ast_threadstorage_get (threadstorage.h:130)
==3112==    by 0x8121961: ast_store_lock_info (utils.c:551)
==3112==    by 0x806BA56: __ast_pthread_mutex_lock (lock.h:369)
==3112==    by 0x806B97C: ast_register_file_version (asterisk.c:281)
==3112==    by 0x812091D: __register_file_version (utils.c:28)
==3112==    by 0x814426A: (within /usr/sbin/asterisk)
==3112==    by 0x8057A6C: (within /usr/sbin/asterisk)
==3112==    by 0x8144138: __libc_csu_init (in /usr/sbin/asterisk)
==3112==    by 0xB2D330: (below main) (in /lib/libc-2.7.so)
==3112==

Thanks Russel for help.
--MohammedShehzad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-dev/attachments/20100106/08906d35/attachment.htm 


More information about the asterisk-dev mailing list