[asterisk-dev] Running a thread

Russell Bryant russell at digium.com
Tue Jan 5 09:57:32 CST 2010


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



More information about the asterisk-dev mailing list