[chuck-users] Reusing threads

Hans Aberg haberg at math.su.se
Tue Jun 9 03:57:30 EDT 2009

On 9 Jun 2009, at 01:22, Robert Poor wrote in 'implementing a "wait  
for signal with timeout"':

>> I am not sure exactly how 'chuck' does memory allocations, but I  
>> avoid allocate any reference types in the threads. I have had the  
>> program running for long time. Are you saying the threads  
>> themselves are leaking?
> Essentially, yes: sporking a shred eats up 88.3K of non-recovered  
> memory.  It's easy to show with this bit of code:

There is an interesting way to reuse threads, as in the attached  
example. Though it is written for Posix threads (pthreads), it might  
be an input to 'chuck' too. Compile it using
   gcc psearch.c -lpthread -o psearch

The program is run as
   ./psearch <string> <file_1> ... <file_n>
and searches for the string <string> in the files named <file_1> ...  
<file_n>. The actual file search is done by search_file(), which is a  
called in sequences in a loop by search(), which in turn is called by  
the main thread which spawns off copies as there is need. The effect  
is that search_file() is called in sequence if there is time for a  
search to finish, but otherwise threads are created according to need  
and max limit.

The example uses mutex locks to make sure the counts get right. But  
your example is simpler - you may not need that.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: psearch.c
Type: application/octet-stream
Size: 3098 bytes
Desc: not available
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20090609/f573fe84/attachment.obj>
-------------- next part --------------

More information about the chuck-users mailing list