LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2006, 08:49 PM   #1
incoming429
LQ Newbie
 
Registered: Apr 2006
Posts: 1

Rep: Reputation: 0
Memory leak when using detached threads


Hello,
I have a program which creates deatached threads. When I
run it with valgrind I get the following message:
==25935== 544 bytes in 4 blocks are possibly lost in loss record 1 of 1
==25935== at 0x4020681: calloc (vg_replace_malloc.c:279)
==25935== by 0x400F93A: (within /lib/ld-2.4.so)
==25935== by 0x400F9E4: _dl_allocate_tls (in /lib/ld-2.4.so)
==25935== by 0x41A2BB6: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/cmov/libpthread-2.4.so)
==25935== by 0x804E1B9: main (rp_main.c:301)

line 301 is where pthread_create() is called.
My only idea of what this can be caused by is that the
threads are somehow killed without being deallocated properly. Maybe the main program finishes before the
thread resources are deallocated but i dont know if this is
possible.

The way I check for finished threads is a global variable
which holds the number of running threads. An exiting
thread decreases the variable by 1 at the end (with a mutex
locked) and the main program quits when it gets to 0.

My question is if this is a good approach or if it can cause this valgrind behaviour. Any help would be
appreciated.
Thank you,
Michal
 
Old 07-14-2006, 09:55 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
There can be all sorts of timing holes. If you care when the threads have finished, then the threads probably should not be detached threads at all.

In my experience, asking threads to own and to manage their own resources is usually a recipe for trouble anyway. As is creating a "potentially unlimited" number of threads.

I'd generally structure the program this way:
  • The main thread initializes the environment, then creates a fixed pool of worker-threads.
  • The worker-threads wait for work (or a termination indicator), do the work, and repeat... staying alive all the time. (A work-queue may build up fron time to time, but the number of employees remains constant.)
  • When it's time to go home, the main thread sends an appropriate indication to all of the children, asking them to terminate themselves.
  • The main thread then reaps (joins) each thread, continuing this process until all of the dead bodies have been reaped. (Note that no signal is required to indicate that a child has died.)
  • The main program cleans up the environment and exits.
"Be nice to your kids." If you structure the program so that things are too asynchronous, and there's a lot of birthing and dying going on, you've got something that's very tough to debug. Very tough to make reliable...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
my process needs a thread counter for detached threads Thinking Programming 6 01-25-2006 03:32 AM
Memory leak?! nyk Programming 10 01-24-2005 07:08 PM
memory leak mfitzpat Linux - Newbie 1 09-24-2004 02:58 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM
Memory Leak FredrikN Linux - General 4 09-17-2002 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration