perl.ithreads http://www.nntp.perl.org/group/perl.ithreads/ ... Copyright 1998-2013 perl.org Mon, 20 May 2013 04:43:50 +0000 ask@perl.org Issues with process forking when 'multithreaded'. by Sander Smeenk Hi,<br/><br/>I&#39;m new on this list, not sure if this is the right place for this,<br/>please redirect me if this is not on topic here. ;)<br/><br/>I am seeing issues with open3()/waitpid() calls when running the same<br/>code as multiple threads - this does not happen with a single thread<br/>running the code - i&#39;m not sure why this happens, but i can reproduce it<br/>so there must be something wrong somewhere. ;)<br/><br/>What am i seeing:<br/><br/>1) waitpid() sometimes returns the $PID for the child process (as if<br/> it was reaped) while the child process is still running.<br/><br/>2) waitpid() sometimes returns -1 instead of the correct values for the<br/> exit state of a child process while the child process ran perfectly<br/> fine. <br/><br/>Again, all this only happens when running multiple &#39;threads&#39;.<br/>Refer to https://8n1.org/raw/8750/71a3 for a codesnipet from the<br/>subroutine which runs as a thread which in turn spawns rsync.<br/><br/>Besides these waitpid() issues, the threaded code seems to run just fine<br/>and no other abnormalities are found.<br/><br/>This is Perl 5.14.2 with ithreads on Ubuntu GNU/Linux, AMD64.<br/>Perhaps this all sounds familiar to any of you? <br/><br/>Please let me know if more input or code is required!<br/><br/>With regards,<br/>-Sander.<br/>-- <br/>| 1 1 was a racehorse, 2 2 was 1 2, 1 1 1 1 race 1 day, 2 2 1 1 2<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1397.html Tue, 05 Mar 2013 03:24:43 +0000 Re: Issues with process forking when 'multithreaded'. by David Mertens No, no, I mean the module called &quot;forks&quot; from cpan:<br/>https://metacpan.org/module/forks#same-API-as-threads<br/><br/>Drop-in replacement for threads, including shared data handling. I but that<br/>would solve your problem satisfactorily.<br/><br/>David<br/><br/><br/>On Mon, Mar 4, 2013 at 10:31 AM, Sander Smeenk &lt;ssmeenk@freshdot.net&gt; wrote:<br/><br/>&gt; Quoting David Mertens (dcmertens.perl@gmail.com):<br/>&gt;<br/>&gt; &gt; Would it be possible to use the &quot;forks&quot; module? It is supposed to<br/>&gt; provide a<br/>&gt; &gt; drop-in replacement for &quot;threads&quot; on operating systems that support posix<br/>&gt; &gt; forks, which would be true in your case. I suspect this would cure your<br/>&gt; &gt; problems, since everything would operate using forks instead of mixing<br/>&gt; &gt; forks and threads.<br/>&gt;<br/>&gt; I &quot;need&quot; the threads because this brings me &#39;:shared&#39; scalars/hashes.<br/>&gt;<br/>&gt; I thought of doing this only with fork() but i&#39;d need to create some<br/>&gt; kind of inter-process-communication thingie to get the results back,<br/>&gt; which isn&#39;t impossible, just requires me to rewrite a large part of<br/>&gt; my code. ;)<br/>&gt;<br/>&gt; And i still don&#39;t understand why mixing threads+fork is &quot;not smart&quot;. ;)<br/>&gt;<br/>&gt; -Sndr.<br/>&gt; --<br/>&gt; | The short fortune teller who escaped from prison: a small medium at<br/>&gt; large.<br/>&gt; | 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/>&gt;<br/><br/><br/><br/>-- <br/> &quot;Debugging is twice as hard as writing the code in the first place.<br/> Therefore, if you write the code as cleverly as possible, you are,<br/> by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1396.html Mon, 04 Mar 2013 22:38:22 +0000 Re: Issues with process forking when 'multithreaded'. by Paul Robert Marino take a look at what I did in MrTools http://sourceforge.net/projects/mrtools/<br/>specifically mrsync.pl<br/>http://mrtools.svn.sourceforge.net/viewvc/mrtools/mrtools/trunk/mrsync.pl?revision=32&amp;view=markup<br/>Its not perfect but it works fairly well I stopped looking at return<br/>codes and just looked at the stderr output to catch errors it worked<br/>pretty much flawlessly for me once I worked out the initial kinks.<br/><br/><br/>On Mon, Mar 4, 2013 at 1:52 PM, Sander Smeenk &lt;ssmeenk@freshdot.net&gt; wrote:<br/>&gt; Quoting Dave Mitchell (davem@iabyn.com):<br/>&gt;<br/>&gt;&gt; &gt; So, it&#39;s not a fork() from the main process (which would perhaps<br/>&gt;&gt; &gt; copy all threads to the fork()ed process, or maybe not, who knows).<br/>&gt;&gt; fork() on linux creates a single thread in the child process, but copies<br/>&gt;&gt; the memory and state of *all* threads. Which is why you can get in a right<br/>&gt;&gt; mess. For example, if one of the other threads had something locked, that<br/>&gt;&gt; will be still be locked in the new process, but without a corresponding<br/>&gt;&gt; thread still running that can unlock it. So the forked process might<br/>&gt;&gt; deadlock or otherwise behave strangely.<br/>&gt;<br/>&gt; Thanks. This is a clear explanation on why you should take care with<br/>&gt; threads and forks. As you might have gathered already i&#39;m not that<br/>&gt; experienced with the internals of threads and fork...<br/>&gt;<br/>&gt; This mail thread (haha!) made clear to me why removing &#39;lock($sharedfoo)&#39;<br/>&gt; calls from the threaded code improved upon the stability. It did indeed<br/>&gt; no longer &#39;lock up&#39; randomly, the above quoted must be exactly why.<br/>&gt; I reworked the code so i no longer needed to lock() things and thought<br/>&gt; to be done with it.<br/>&gt;<br/>&gt;<br/>&gt;&gt; This is why people say not to mix threads and forks.<br/>&gt;<br/>&gt; I honestly swear i will rewrite my production code using fork() only. :-)<br/>&gt;<br/>&gt; Still, out of curiosity, i would like to keep discussing this (and<br/>&gt; fiddle with the thread+fork mix some more). I&#39;m fairly certain that my<br/>&gt; threaded code is not creating locks anymore. I&#39;ve ran the code<br/>&gt; uncountable times now and it has never locked up (yet).<br/>&gt;<br/>&gt; Locks aren&#39;t really my problem, it is waitpid() that is &#39;acting up&#39; and<br/>&gt; i am unable to find an explanation for that.<br/>&gt;<br/>&gt; Is it true that even though each thread has it&#39;s own &#39;safe&#39; memory to<br/>&gt; work with, fork()ing from two threads at separate times<br/>&gt; changes (reinitialises?) memorystructures in either thread?<br/>&gt;<br/>&gt; For example, can a succesfull fork() (or waitpid()?) in thread2 cause<br/>&gt; the waitpid() in thread1 to return too? Even though the childprocess<br/>&gt; &#39;in&#39; thread1 is still running?<br/>&gt;<br/>&gt;<br/>&gt; Thanks a bundle so far!<br/>&gt; Still learning every day. :)<br/>&gt;<br/>&gt;<br/>&gt; -Sndr.<br/>&gt; --<br/>&gt; | The world is so full of these wonderful things,<br/>&gt; | i&#39;m sure we should all be as happy as kings.<br/>&gt; | 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1395.html Mon, 04 Mar 2013 19:09:07 +0000 Re: Issues with process forking when 'multithreaded'. by Sander Smeenk Quoting Dave Mitchell (davem@iabyn.com):<br/><br/>&gt; &gt; So, it&#39;s not a fork() from the main process (which would perhaps<br/>&gt; &gt; copy all threads to the fork()ed process, or maybe not, who knows).<br/>&gt; fork() on linux creates a single thread in the child process, but copies<br/>&gt; the memory and state of *all* threads. Which is why you can get in a right<br/>&gt; mess. For example, if one of the other threads had something locked, that<br/>&gt; will be still be locked in the new process, but without a corresponding<br/>&gt; thread still running that can unlock it. So the forked process might<br/>&gt; deadlock or otherwise behave strangely.<br/><br/>Thanks. This is a clear explanation on why you should take care with<br/>threads and forks. As you might have gathered already i&#39;m not that<br/>experienced with the internals of threads and fork...<br/><br/>This mail thread (haha!) made clear to me why removing &#39;lock($sharedfoo)&#39;<br/>calls from the threaded code improved upon the stability. It did indeed<br/>no longer &#39;lock up&#39; randomly, the above quoted must be exactly why.<br/>I reworked the code so i no longer needed to lock() things and thought<br/>to be done with it.<br/><br/><br/>&gt; This is why people say not to mix threads and forks.<br/><br/>I honestly swear i will rewrite my production code using fork() only. :-)<br/><br/>Still, out of curiosity, i would like to keep discussing this (and<br/>fiddle with the thread+fork mix some more). I&#39;m fairly certain that my<br/>threaded code is not creating locks anymore. I&#39;ve ran the code<br/>uncountable times now and it has never locked up (yet).<br/><br/>Locks aren&#39;t really my problem, it is waitpid() that is &#39;acting up&#39; and<br/>i am unable to find an explanation for that.<br/><br/>Is it true that even though each thread has it&#39;s own &#39;safe&#39; memory to<br/>work with, fork()ing from two threads at separate times<br/>changes (reinitialises?) memorystructures in either thread?<br/><br/>For example, can a succesfull fork() (or waitpid()?) in thread2 cause<br/>the waitpid() in thread1 to return too? Even though the childprocess<br/>&#39;in&#39; thread1 is still running?<br/><br/><br/>Thanks a bundle so far!<br/>Still learning every day. :)<br/><br/><br/>-Sndr.<br/>-- <br/>| The world is so full of these wonderful things,<br/>| i&#39;m sure we should all be as happy as kings.<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1394.html Mon, 04 Mar 2013 18:52:14 +0000 Re: Issues with process forking when 'multithreaded'. by Sander Smeenk Quoting Mike Pomraning (mjp@pilcrow.madison.wi.us):<br/><br/>&gt; &gt; And i still don&#39;t understand why mixing threads+fork is &quot;not smart&quot;. ;)<br/>&gt; Mixing the two makes it easy to introduce damning error.<br/>&gt; <br/>&gt; Here&#39;s someone who learned it the hard way:<br/>&gt; http://rachelbythebay.com/w/2011/06/07/forked/<br/>&gt; <br/>&gt; Here are some caveats on forking and program state:<br/>&gt; http://pubs.opengroup.org/onlinepubs/009696799/functions/pthread_atfork.html#tag_03_486_08<br/>&gt; <br/>&gt; Here&#39;s an acknowledgement that the standards folks didn&#39;t get all the pesky<br/>&gt; semantics quite right:<br/>&gt; https://collaboration.opengroup.org/external/pasc.org/interpretations/unofficial/db/p1003.1c/pasc-1003.1c-37.html<br/><br/>Thanks. This was insightful. It&#39;s hard to find these links using Google.<br/>All you find is people experiencing deadlocks and other problems. ;)<br/><br/>-Sndr.<br/>-- <br/>| Today is the first day of the rest of your life<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1393.html Mon, 04 Mar 2013 18:13:49 +0000 Re: Issues with process forking when 'multithreaded'. by Dave Mitchell On Mon, Mar 04, 2013 at 04:32:13PM +0100, Sander Smeenk wrote:<br/>&gt; So, it&#39;s not a fork() from the main process (which would perhaps<br/>&gt; copy all threads to the fork()ed process, or maybe not, who knows).<br/><br/>fork() on linux creates a single thread in the child process, but copies<br/>the memory and state of *all* threads. Which is why you can get in a right<br/>mess. For example, if one of the other threads had something locked, that<br/>will be still be locked in the new process, but without a corresponding<br/>thread still running that can unlock it. So the forked process might<br/>deadlock or otherwise behave strangely.<br/><br/>This is why people say not to mix threads and forks.<br/><br/>-- <br/>My Dad used to say &#39;always fight fire with fire&#39;, which is probably why<br/>he got thrown out of the fire brigade.<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1392.html Mon, 04 Mar 2013 17:00:57 +0000 Re: Issues with process forking when 'multithreaded'. by Mike Pomraning On Mon, Mar 4, 2013 at 10:31 AM, Sander Smeenk &lt;ssmeenk@freshdot.net&gt; wrote:<br/><br/>&gt;<br/>&gt; And i still don&#39;t understand why mixing threads+fork is &quot;not smart&quot;. ;)<br/>&gt;<br/><br/>Mixing the two makes it easy to introduce damning error.<br/><br/>Here&#39;s someone who learned it the hard way:<br/>http://rachelbythebay.com/w/2011/06/07/forked/<br/><br/>Here are some caveats on forking and program state:<br/>http://pubs.opengroup.org/onlinepubs/009696799/functions/pthread_atfork.html#tag_03_486_08<br/><br/>Here&#39;s an acknowledgement that the standards folks didn&#39;t get all the pesky<br/>semantics quite right:<br/>https://collaboration.opengroup.org/external/pasc.org/interpretations/unofficial/db/p1003.1c/pasc-1003.1c-37.html<br/><br/>-Mike<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1391.html Mon, 04 Mar 2013 16:46:22 +0000 Re: Issues with process forking when 'multithreaded'. by Sander Smeenk Quoting David Mertens (dcmertens.perl@gmail.com):<br/><br/>&gt; Would it be possible to use the &quot;forks&quot; module? It is supposed to provide a<br/>&gt; drop-in replacement for &quot;threads&quot; on operating systems that support posix<br/>&gt; forks, which would be true in your case. I suspect this would cure your<br/>&gt; problems, since everything would operate using forks instead of mixing<br/>&gt; forks and threads.<br/><br/>I &quot;need&quot; the threads because this brings me &#39;:shared&#39; scalars/hashes.<br/><br/>I thought of doing this only with fork() but i&#39;d need to create some<br/>kind of inter-process-communication thingie to get the results back,<br/>which isn&#39;t impossible, just requires me to rewrite a large part of<br/>my code. ;)<br/><br/>And i still don&#39;t understand why mixing threads+fork is &quot;not smart&quot;. ;)<br/><br/>-Sndr.<br/>-- <br/>| The short fortune teller who escaped from prison: a small medium at large.<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1390.html Mon, 04 Mar 2013 16:32:11 +0000 Re: Issues with process forking when 'multithreaded'. by David Mertens Sander,<br/><br/>Would it be possible to use the &quot;forks&quot; module? It is supposed to provide a<br/>drop-in replacement for &quot;threads&quot; on operating systems that support posix<br/>forks, which would be true in your case. I suspect this would cure your<br/>problems, since everything would operate using forks instead of mixing<br/>forks and threads.<br/><br/>As for the details of why this causes trouble, I am not yet familiar enough<br/>with the internals of Perl&#39;s threading implementation to answer. Sorry.<br/><br/>David<br/>On Mar 4, 2013 9:32 AM, &quot;Sander Smeenk&quot; &lt;ssmeenk@freshdot.net&gt; wrote:<br/><br/>&gt; Quoting Sander Smeenk (ssmeenk@freshdot.net):<br/>&gt;<br/>&gt; &gt; In fact, it does work, for 95% of all threads it works as designed, only<br/>&gt; &gt; a select few (but not the same) threads fail each time, i was hoping to<br/>&gt; &gt; find out why only this small portion of threads fail their waitpid()<br/>&gt; call. ;)<br/>&gt;<br/>&gt; Also, this might not be clear from my original message, the main process<br/>&gt; is the process that creates the threads, one for each server that needs<br/>&gt; to be backed up. Each thread calls fork() only once (through open3) for<br/>&gt; the rsync to do the heavy lifting.<br/>&gt;<br/>&gt; So, it&#39;s not a fork() from the main process (which would perhaps<br/>&gt; copy all threads to the fork()ed process, or maybe not, who knows).<br/>&gt;<br/>&gt; Amazing there is so little known about what this does / how this works,<br/>&gt; i&#39;ll see if i can get a working testcase instead of the snipped i<br/>&gt; pasted.<br/>&gt;<br/>&gt; -Sndr.<br/>&gt; --<br/>&gt; | One tequila, two tequila, three tequila, floor.<br/>&gt; | 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1389.html Mon, 04 Mar 2013 16:01:20 +0000 Re: Issues with process forking when 'multithreaded'. by Sander Smeenk Quoting Sander Smeenk (ssmeenk@freshdot.net):<br/><br/>&gt; In fact, it does work, for 95% of all threads it works as designed, only<br/>&gt; a select few (but not the same) threads fail each time, i was hoping to<br/>&gt; find out why only this small portion of threads fail their waitpid() call. ;)<br/><br/>Also, this might not be clear from my original message, the main process<br/>is the process that creates the threads, one for each server that needs<br/>to be backed up. Each thread calls fork() only once (through open3) for<br/>the rsync to do the heavy lifting.<br/><br/>So, it&#39;s not a fork() from the main process (which would perhaps<br/>copy all threads to the fork()ed process, or maybe not, who knows).<br/><br/>Amazing there is so little known about what this does / how this works,<br/>i&#39;ll see if i can get a working testcase instead of the snipped i<br/>pasted.<br/><br/>-Sndr.<br/>-- <br/>| One tequila, two tequila, three tequila, floor.<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1388.html Mon, 04 Mar 2013 15:32:29 +0000 Re: Issues with process forking when 'multithreaded'. by Sander Smeenk Quoting Jerry D. Hedden (jerry@hedden.us):<br/><br/>&gt; For the most part, threads and forks are not compatible. Therefore, you<br/>&gt; probably cannot use open3()/waitpid() in a threaded application. If you<br/>&gt; google &#39;perl fork threads&#39;, you&#39;ll find lots more info.<br/><br/>I kind of expected this reply. :&gt;<br/><br/>I did read about &quot;it not being smart to mix fork() and threads&quot;. This<br/>comes down to portability issues and unexpected results on different<br/>platforms. With this project i don&#39;t care about portability.<br/>This should run on my GNU/Linux AMD64 system only. :)<br/><br/>At the same time fork()ing from a thread is allowed, doesn&#39;t raise<br/>warnings and no-one can tell me exactly why it can not work (everyone<br/>tells me &#39;it is not smart&#39;). <br/><br/>In fact, it does work, for 95% of all threads it works as designed, only<br/>a select few (but not the same) threads fail each time, i was hoping to<br/>find out why only this small portion of threads fail their waitpid() call. ;)<br/><br/>I&#39;ll start reworking this with pure fork()s. Have to look into some IPC<br/>way of getting the output from the childs back in the main process.<br/><br/>Thanks so far,<br/>-Sndr.<br/>-- <br/>| Man who lose watch in toilet bound to have shitty time.<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1387.html Mon, 04 Mar 2013 15:17:28 +0000 Re: Issues with process forking when 'multithreaded'. by Jerry D. Hedden For the most part, threads and forks are not compatible. Therefore, you<br/>probably cannot use open3()/waitpid() in a threaded application. If you<br/>google &#39;perl fork threads&#39;, you&#39;ll find lots more info.<br/><br/><br/>On Mon, Mar 4, 2013 at 9:25 AM, Sander Smeenk &lt;ssmeenk@freshdot.net&gt; wrote:<br/><br/>&gt; Hi,<br/>&gt;<br/>&gt; I&#39;m new on this list, not sure if this is the right place for this,<br/>&gt; please redirect me if this is not on topic here. ;)<br/>&gt;<br/>&gt; I am seeing issues with open3()/waitpid() calls when running the same<br/>&gt; code as multiple threads - this does not happen with a single thread<br/>&gt; running the code - i&#39;m not sure why this happens, but i can reproduce it<br/>&gt; so there must be something wrong somewhere. ;)<br/>&gt;<br/>&gt; What am i seeing:<br/>&gt;<br/>&gt; 1) waitpid() sometimes returns the $PID for the child process (as if<br/>&gt; it was reaped) while the child process is still running.<br/>&gt;<br/>&gt; 2) waitpid() sometimes returns -1 instead of the correct values for the<br/>&gt; exit state of a child process while the child process ran perfectly<br/>&gt; fine.<br/>&gt;<br/>&gt; Again, all this only happens when running multiple &#39;threads&#39;.<br/>&gt; Refer to https://8n1.org/raw/8750/71a3 for a codesnipet from the<br/>&gt; subroutine which runs as a thread which in turn spawns rsync.<br/>&gt;<br/>&gt; Besides these waitpid() issues, the threaded code seems to run just fine<br/>&gt; and no other abnormalities are found.<br/>&gt;<br/>&gt; This is Perl 5.14.2 with ithreads on Ubuntu GNU/Linux, AMD64.<br/>&gt; Perhaps this all sounds familiar to any of you?<br/>&gt;<br/>&gt; Please let me know if more input or code is required!<br/>&gt;<br/>&gt; With regards,<br/>&gt; -Sander.<br/>&gt; --<br/>&gt; | 1 1 was a racehorse, 2 2 was 1 2, 1 1 1 1 race 1 day, 2 2 1 1 2<br/>&gt; | 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1386.html Mon, 04 Mar 2013 14:51:51 +0000 Issues with process forking when 'multithreaded'. by Sander Smeenk Hi,<br/><br/>I&#39;m new on this list, not sure if this is the right place for this,<br/>please redirect me if this is not on topic here. ;)<br/><br/>I am seeing issues with open3()/waitpid() calls when running the same<br/>code as multiple threads - this does not happen with a single thread<br/>running the code - i&#39;m not sure why this happens, but i can reproduce it<br/>so there must be something wrong somewhere. ;)<br/><br/>What am i seeing:<br/><br/>1) waitpid() sometimes returns the $PID for the child process (as if<br/> it was reaped) while the child process is still running.<br/><br/>2) waitpid() sometimes returns -1 instead of the correct values for the<br/> exit state of a child process while the child process ran perfectly<br/> fine. <br/><br/>Again, all this only happens when running multiple &#39;threads&#39;.<br/>Refer to https://8n1.org/raw/8750/71a3 for a codesnipet from the<br/>subroutine which runs as a thread which in turn spawns rsync.<br/><br/>Besides these waitpid() issues, the threaded code seems to run just fine<br/>and no other abnormalities are found.<br/><br/>This is Perl 5.14.2 with ithreads on Ubuntu GNU/Linux, AMD64.<br/>Perhaps this all sounds familiar to any of you? <br/><br/>Please let me know if more input or code is required!<br/><br/>With regards,<br/>-Sander.<br/>-- <br/>| 1 1 was a racehorse, 2 2 was 1 2, 1 1 1 1 race 1 day, 2 2 1 1 2<br/>| 4096R/20CC6CD2 - 6D40 1A20 B9AA 87D4 84C7 FBD6 F3A9 9442 20CC 6CD2<br/> http://www.nntp.perl.org/group/perl.ithreads/2013/03/msg1385.html Mon, 04 Mar 2013 14:25:49 +0000 Re: Undefined thread object for main thread (tid zero) even thoughthe main thread exists by David Mertens An update:<br/><br/>I added a special case for thread id 0, though this is not ideal. For<br/>better or for worse, my module does not provide any thread entry points. It<br/>simply provides an unconventional means for sharing data across threads.<br/>This means that a user of this module could launch an async{} block that<br/>pulls in the shared data and modifies it. It also means that they can never<br/>share data that originated in an async block.<br/><br/>I&#39;ve thoroughly documented all of this and I don&#39;t think this will be too<br/>much of a problem for most users. However, if I could get some way, even an<br/>XS-based way, to know if a thread is still alive, that would be quite handy.<br/><br/>Thanks!<br/>David<br/><br/><br/>On Tue, Dec 4, 2012 at 10:02 PM, Mike Pomraning<br/>&lt;mjp@pilcrow.madison.wi.us&gt;wrote:<br/><br/>&gt; On Tue, Dec 4, 2012 at 9:15 PM, David Mertens &lt;dcmertens.perl@gmail.com&gt;wrote:<br/>&gt;<br/>&gt;&gt; Thanks for the explanation. Can you think of any way (apart from hackery)<br/>&gt;&gt; that I could check that a detached thread with the given tid exists? I do<br/>&gt;&gt; not need to manipulate it, I just need to know if it exists. I might<br/>&gt;&gt; consider tracking the existing threads through a shared array, but then I<br/>&gt;&gt; would have to modify the content of that shared array in an END block,<br/>&gt;&gt; which seems to be discouraged &lt;&lt;SNIP&gt;&gt;<br/>&gt;&gt;<br/>&gt;<br/>&gt; You could wrap every user-supplied entry point function in your own<br/>&gt; enclosing, anonymous sub for bookkeeping. This sub is the true entry point<br/>&gt; passed to threads-&gt;create(), and it touches `our %Tids : shared` before and<br/>&gt; after invoking the user-supplied sub. Thread 0 is still a special case.<br/>&gt;<br/>&gt; Liz&#39;s Thread::Exit has an example of this approach.<br/>&gt;<br/>&gt; -Mike<br/>&gt;<br/><br/><br/><br/>-- <br/> &quot;Debugging is twice as hard as writing the code in the first place.<br/> Therefore, if you write the code as cleverly as possible, you are,<br/> by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2013/01/msg1384.html Wed, 02 Jan 2013 23:26:03 +0000 Re: [Pdl-porters] RFC: PDL::Parallel::threads andPDL::Parallel::threads::SIMD by jns-cmarshall Hi David-<br/><br/>I&#39;m looking forward to taking a look. :-)<br/><br/>This reminds me of the MCE (Many Core Engine)<br/>module to support multiple core perl processing.<br/>I wonder if it could be used for PDL processing?<br/><br/>--Chris<br/><br/>----- David Mertens &lt;dcmertens.perl@gmail.com&gt; wrote:<br/>&gt; Hello everybody -<br/>&gt; <br/>&gt; PDL folks will know that I have a lot of projects running lately, but I<br/>&gt; felt compelled to push this one through at the moment. Threads folks are<br/>&gt; probably aware that I&#39;ve been working on something lately and I now have<br/>&gt; something that is hopefully worthy of your close scrutiny. I would<br/>&gt; appreciate any feedback for the two modules listed in the subject line,<br/>&gt; both of which shall be distributed on CPAN under PDL::Parallel::threads.<br/>&gt; <br/>&gt; PDL::Parallel::threads provides a means for sharing PDL data across Perl<br/>&gt; threads. Presently, it is not safe to launch multiple threads in Perl after<br/>&gt; loading PDL. (A simple CLONE_SKIP fixes this, as implemented.) It makes<br/>&gt; sharing of physical piddles and memory mapped files easy. I have hit some<br/>&gt; bumps with memory mapping on Windows, but I expect that&#39;ll get ironed out<br/>&gt; in the coming weeks and months. I can provide more feedback there if it<br/>&gt; would be helpful.<br/>&gt; <br/>&gt; PDL::Parallel::threads::SIMD provides a very simple interface (not<br/>&gt; stress-tested) for launching groups of parallel processes with the<br/>&gt; capability of performing barrier synchronization. This could almost serve<br/>&gt; as a stand-alone Threads module, but I tend to affiliate SIMD stuff with<br/>&gt; numerical computing, plus I use it in one or two of my tests. Perhaps when<br/>&gt; PDL::Parallel::threads gets pulled into PDL, I can spin off ...::SIMD into<br/>&gt; Threads::SIMD or some such.<br/>&gt; <br/>&gt; I just uploaded the distribution to PAUSE, so expect it to hit CPAN in the<br/>&gt; next 12-24 hours. You can examine the code on github:<br/>&gt; https://github.com/run4flat/PDL-Parallel-threads<br/>&gt; <br/>&gt; Thoughts and comments are welcome. PDL folks, I&#39;d like to eventually move<br/>&gt; this stuff into PDL&#39;s core, so the more feedback I can get on it, the<br/>&gt; better.<br/>&gt; <br/>&gt; Thanks!<br/>&gt; David<br/>&gt; <br/>&gt; -- <br/>&gt; &quot;Debugging is twice as hard as writing the code in the first place.<br/>&gt; Therefore, if you write the code as cleverly as possible, you are,<br/>&gt; by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1383.html Sun, 16 Dec 2012 07:14:02 +0000 Re: pool example by Jerry D. Hedden The timeout is 10. A thread may sleep from 5 to 15 seconds. So some will<br/>end before getting killed.<br/><br/>Also, the timer thread does not know how long the sleep value in a thread<br/>is.<br/><br/>Again, this is an example of pooling. Obviously, a thread that just sleeps<br/>is useless. The WORK section might do DB or web searches. In that case,<br/>you end result would be the main thread getting whatever results had been<br/>obtained during the timeout period.<br/><br/><br/>On Thu, Dec 13, 2012 at 12:15 PM, Alfonso Caponi<br/>&lt;alfonso.caponi@gmail.com&gt;wrote:<br/><br/>&gt; Hi list,<br/>&gt;<br/>&gt; I&#39;ve a question about pool example script (<br/>&gt; http://cpansearch.perl.org/src/JDHEDDEN/threads-1.86/examples/pool.pl).<br/>&gt;<br/>&gt; Theoretically, should the sub &quot;timer&quot; kill threads immediately when sleep<br/>&gt; value is greater than timeout value?<br/>&gt; At the moment seems that the threads wait for the sleep value than they got<br/>&gt; killed by sub &quot;timer&quot;.<br/>&gt;<br/>&gt; Thank you<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1382.html Thu, 13 Dec 2012 17:35:26 +0000 pool example by Alfonso Caponi Hi list,<br/><br/>I&#39;ve a question about pool example script (<br/>http://cpansearch.perl.org/src/JDHEDDEN/threads-1.86/examples/pool.pl).<br/><br/>Theoretically, should the sub &quot;timer&quot; kill threads immediately when sleep<br/>value is greater than timeout value?<br/>At the moment seems that the threads wait for the sleep value than they got<br/>killed by sub &quot;timer&quot;.<br/><br/>Thank you<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1381.html Thu, 13 Dec 2012 17:15:47 +0000 RFC: PDL::Parallel::threads and PDL::Parallel::threads::SIMD by David Mertens Hello everybody -<br/><br/>PDL folks will know that I have a lot of projects running lately, but I<br/>felt compelled to push this one through at the moment. Threads folks are<br/>probably aware that I&#39;ve been working on something lately and I now have<br/>something that is hopefully worthy of your close scrutiny. I would<br/>appreciate any feedback for the two modules listed in the subject line,<br/>both of which shall be distributed on CPAN under PDL::Parallel::threads.<br/><br/>PDL::Parallel::threads provides a means for sharing PDL data across Perl<br/>threads. Presently, it is not safe to launch multiple threads in Perl after<br/>loading PDL. (A simple CLONE_SKIP fixes this, as implemented.) It makes<br/>sharing of physical piddles and memory mapped files easy. I have hit some<br/>bumps with memory mapping on Windows, but I expect that&#39;ll get ironed out<br/>in the coming weeks and months. I can provide more feedback there if it<br/>would be helpful.<br/><br/>PDL::Parallel::threads::SIMD provides a very simple interface (not<br/>stress-tested) for launching groups of parallel processes with the<br/>capability of performing barrier synchronization. This could almost serve<br/>as a stand-alone Threads module, but I tend to affiliate SIMD stuff with<br/>numerical computing, plus I use it in one or two of my tests. Perhaps when<br/>PDL::Parallel::threads gets pulled into PDL, I can spin off ...::SIMD into<br/>Threads::SIMD or some such.<br/><br/>I just uploaded the distribution to PAUSE, so expect it to hit CPAN in the<br/>next 12-24 hours. You can examine the code on github:<br/>https://github.com/run4flat/PDL-Parallel-threads<br/><br/>Thoughts and comments are welcome. PDL folks, I&#39;d like to eventually move<br/>this stuff into PDL&#39;s core, so the more feedback I can get on it, the<br/>better.<br/><br/>Thanks!<br/>David<br/><br/>-- <br/> &quot;Debugging is twice as hard as writing the code in the first place.<br/> Therefore, if you write the code as cleverly as possible, you are,<br/> by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1380.html Thu, 13 Dec 2012 07:19:40 +0000 Re: Undefined thread object for main thread (tid zero) even thoughthe main thread exists by Mike Pomraning On Tue, Dec 4, 2012 at 9:15 PM, David Mertens &lt;dcmertens.perl@gmail.com&gt;wrote:<br/><br/>&gt; Thanks for the explanation. Can you think of any way (apart from hackery)<br/>&gt; that I could check that a detached thread with the given tid exists? I do<br/>&gt; not need to manipulate it, I just need to know if it exists. I might<br/>&gt; consider tracking the existing threads through a shared array, but then I<br/>&gt; would have to modify the content of that shared array in an END block,<br/>&gt; which seems to be discouraged &lt;&lt;SNIP&gt;&gt;<br/>&gt;<br/><br/>You could wrap every user-supplied entry point function in your own<br/>enclosing, anonymous sub for bookkeeping. This sub is the true entry point<br/>passed to threads-&gt;create(), and it touches `our %Tids : shared` before and<br/>after invoking the user-supplied sub. Thread 0 is still a special case.<br/><br/>Liz&#39;s Thread::Exit has an example of this approach.<br/><br/>-Mike<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1379.html Wed, 05 Dec 2012 04:02:56 +0000 Re: Undefined thread object for main thread (tid zero) even thoughthe main thread exists by David Mertens Mike -<br/><br/>Thanks for the explanation. Can you think of any way (apart from hackery)<br/>that I could check that a detached thread with the given tid exists? I do<br/>not need to manipulate it, I just need to know if it exists. I might<br/>consider tracking the existing threads through a shared array, but then I<br/>would have to modify the content of that shared array in an END block,<br/>which seems to be discouraged here:<br/>http://perldoc.perl.org/threads.html#BUGS-AND-LIMITATIONS. At any rate, I<br/>will soften the condition on thread existence to allow for the zeroeth<br/>thread to pass through.<br/><br/>I think I have enough of an answer to move forward for now. However, ideas<br/>and comments are welcome! Also, I would be more than happy to discuss the<br/>details if anybody would like to hear. I&#39;m going to begin, however, by<br/>presuming that the list doesn&#39;t want too much noise. :-)<br/><br/>github: https://github.com/run4flat/PDL-Parallel-threads<br/><br/>David<br/><br/><br/>On Mon, Dec 3, 2012 at 8:47 AM, Mike Pomraning &lt;mjp@pilcrow.madison.wi.us&gt;wrote:<br/><br/>&gt; On Sun, Dec 2, 2012 at 10:53 PM, David Mertens &lt;dcmertens.perl@gmail.com&gt;wrote:<br/>&gt;<br/>&gt;&gt; ----%&lt;----<br/>&gt;&gt;<br/>&gt;&gt; defined (threads-&gt;object($originating_tid{$name}))<br/>&gt;&gt; or croak(&quot;retrieve_pdls: &#39;$name&#39; was created in a thread that &quot;<br/>&gt;&gt; . &quot;is no longer available&quot;);<br/>&gt;&gt;<br/>&gt;&gt; ----&gt;%----<br/>&gt;&gt;<br/>&gt;&gt; In my test suite, I create and store data in the main thread. For this<br/>&gt;&gt; example, then, $originating_tid{$name} == 0 for all cases considered. The<br/>&gt;&gt; problem is that only the main thread is capable of retrieving the threads<br/>&gt;&gt; object for tid 0. Any other (child) thread gets an undefined value. Does<br/>&gt;&gt; that surprise anyone? Are child threads able to get the thread objects for<br/>&gt;&gt; their parents or siblings?<br/>&gt;&gt;<br/>&gt;<br/>&gt; I might be misreading this, but I believe that threads-&gt;object() ignores<br/>&gt; detached threads unless a tid is explicitly passed in *and* that tid is the<br/>&gt; tid of the calling thread. The main thread is detached, so no thread other<br/>&gt; than tid 0 can look it up via object().<br/>&gt;<br/>&gt; (You describe an interesting problem and approach; do you have<br/>&gt; demonstration code that others can take a look at?)<br/>&gt;<br/>&gt; -Mike<br/>&gt;<br/><br/><br/><br/>-- <br/> &quot;Debugging is twice as hard as writing the code in the first place.<br/> Therefore, if you write the code as cleverly as possible, you are,<br/> by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1378.html Wed, 05 Dec 2012 03:15:21 +0000 Re: Undefined thread object for main thread (tid zero) even thoughthe main thread exists by Mike Pomraning On Sun, Dec 2, 2012 at 10:53 PM, David Mertens &lt;dcmertens.perl@gmail.com&gt;wrote:<br/><br/>&gt; ----%&lt;----<br/>&gt;<br/>&gt; defined (threads-&gt;object($originating_tid{$name}))<br/>&gt; or croak(&quot;retrieve_pdls: &#39;$name&#39; was created in a thread that &quot;<br/>&gt; . &quot;is no longer available&quot;);<br/>&gt;<br/>&gt; ----&gt;%----<br/>&gt;<br/>&gt; In my test suite, I create and store data in the main thread. For this<br/>&gt; example, then, $originating_tid{$name} == 0 for all cases considered. The<br/>&gt; problem is that only the main thread is capable of retrieving the threads<br/>&gt; object for tid 0. Any other (child) thread gets an undefined value. Does<br/>&gt; that surprise anyone? Are child threads able to get the thread objects for<br/>&gt; their parents or siblings?<br/>&gt;<br/><br/>I might be misreading this, but I believe that threads-&gt;object() ignores<br/>detached threads unless a tid is explicitly passed in *and* that tid is the<br/>tid of the calling thread. The main thread is detached, so no thread other<br/>than tid 0 can look it up via object().<br/><br/>(You describe an interesting problem and approach; do you have<br/>demonstration code that others can take a look at?)<br/><br/>-Mike<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1377.html Mon, 03 Dec 2012 15:25:18 +0000 Undefined thread object for main thread (tid zero) even though themain thread exists by David Mertens Hey folks -<br/><br/>I am working on a module to share PDL data (C-like arrays of data) across<br/>threads. It pretty much works, but it&#39;s not quite as rock-solid as I&#39;d<br/>like. I&#39;d first like to solve my immediate problem, then perhaps open the<br/>larger discussion of how one shares (scalar) objects whose guts are<br/>actually C structs. (I am interested in this both for use in Prima as well<br/>as PDL, and likely other modules that I will come across or write. But I&#39;m<br/>getting ahead of myself.)<br/><br/>My current module works by letting users share PDL *data*, rather than<br/>sharing the piddle itself. Since I cannot share the actual piddle (due to<br/>the difficulties with sharing C structs across threads), I simply tick up<br/>the original piddle&#39;s refcount and store the data pointer in a shared Perl<br/>hash. Then, when the user requests a shared copy of that data in a thread,<br/>I create a *very* thin piddle wrapper around the C array of the original<br/>piddle. Believe it or not, it actually works! With any luck, I may even be<br/>able to use this for real multicore high-performance computing stuff.<br/><br/>However, life being what it is, there are corner cases. If that original<br/>piddle&#39;s thread terminates, the interpreter frees the memory and the data<br/>goes away. Any wrapped piddles in other threads are now ticking<br/>segmentation faults just waiting for a data read/write.<br/><br/>To help prevent that situation, I decided to store the thread id in which a<br/>piddle&#39;s data is first shared. At the very least, this would let me check<br/>that the data&#39;s originating thread is still around, and croak if this were<br/>not the case. That code looks something like this:<br/><br/>----%&lt;----<br/><br/>defined (threads-&gt;object($originating_tid{$name}))<br/> or croak(&quot;retrieve_pdls: &#39;$name&#39; was created in a thread that &quot;<br/> . &quot;is no longer available&quot;);<br/><br/>----&gt;%----<br/><br/>In my test suite, I create and store data in the main thread. For this<br/>example, then, $originating_tid{$name} == 0 for all cases considered. The<br/>problem is that only the main thread is capable of retrieving the threads<br/>object for tid 0. Any other (child) thread gets an undefined value. Does<br/>that surprise anyone? Are child threads able to get the thread objects for<br/>their parents or siblings?<br/><br/>Thanks!<br/>David<br/><br/>-- <br/> &quot;Debugging is twice as hard as writing the code in the first place.<br/> Therefore, if you write the code as cleverly as possible, you are,<br/> by definition, not smart enough to debug it.&quot; -- Brian Kernighan<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2012/12/msg1376.html Mon, 03 Dec 2012 04:53:58 +0000 Hello by liz Mail transaction failed. Partial message is available.<br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2011/11/msg1375.html Sat, 19 Nov 2011 22:17:00 +0000 Re: timing out threads by Mike Pomraning On Tue, Nov 1, 2011 at 6:09 PM, Danny Wong (dannwong) &lt;dannwong@cisco.com&gt;wrote:<br/><br/>&gt; I would like to perform the following, start X (say 10) number<br/>&gt; of threads to perform some operation (ex. Copy command), I would like to<br/>&gt; exit or return out of the thread(s) that takes too long ( ex. 30 minutes<br/>&gt; ).<br/><br/><br/>Danny,<br/><br/>Can you show us in more detail what the long-running threads are doing,<br/>perhaps some pseudocode? How one safely kills/interrupts/shuts down a<br/>thread depends on what that thread is doing.<br/><br/>As this forum discussed a little over a year ago, threads stuck in blocking<br/>system calls present their own design challenges.<br/><br/>-Mike<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2011/11/msg1374.html Wed, 02 Nov 2011 06:23:07 +0000 Re: timing out threads by Francesco Nidito Hi Danny,<br/><br/>You can just add a counter for each thread and propagate the counter to the<br/>main thread at the end of the computation.<br/><br/>For instance, a hash local to each thread mapping from &quot;job id&quot; (in your<br/>case the name of the file) and the time. At the end of the execution, you<br/>can send the counters back to the main thread though a Queue and let the<br/>main thread do some statistics and output the results.<br/><br/>I pseudocode:<br/><br/>task queue Q;<br/>counters queue C;<br/><br/>thread {<br/> create counter c_X;<br/><br/> while there is &quot;something&quot; in Q<br/> do &quot;something&quot;;<br/> c_X{something} = time required to do &quot;something&quot;;<br/><br/><br/> enqueue c_X in C;<br/>}<br/><br/>main {<br/> create N threads;<br/> load Q with the tasks to be done;<br/> wait the N thread to finish (join);<br/><br/> get all the counters c_i from C;<br/> do something with c_1 ... c_N<br/>}<br/><br/>Hope this helps!<br/><br/>Cheers,<br/>nids<br/><br/> ------------------------------<br/>&laquo;Fear is not what&#39;s important; it&#39;s how you deal with it. It would be like<br/>asking a marathon runner if they feel pain. It&#39;s not a matter of whether<br/>you feel it; it&#39;s how you manage it.&raquo;<br/>&mdash; James Nachtwey<br/><br/><br/><br/>On Tue, Nov 1, 2011 at 11:09 PM, Danny Wong (dannwong)<br/>&lt;dannwong@cisco.com&gt;wrote:<br/><br/>&gt; Hi Thread GURU,<br/>&gt; I would like to perform the following, start X (say 10) number<br/>&gt; of threads to perform some operation (ex. Copy command), I would like to<br/>&gt; exit or return out of the thread(s) that takes too long ( ex. 30 minutes<br/>&gt; ). I&#39;m thinking I should spawn an independent thread (a watch thread)<br/>&gt; and have that thread watch to see if any of the 10 threads are taking<br/>&gt; longer than 30 minutes to complete. Any example how I can accomplish<br/>&gt; this? Any other suggesting would be great to.<br/>&gt;<br/>&gt; BTW, I&#39;m using perl 5.8.8., thread version 1.07.<br/>&gt;<br/>&gt; Thanks.<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2011/11/msg1373.html Wed, 02 Nov 2011 05:05:49 +0000 timing out threads by Danny Wong Hi Thread GURU,<br/> I would like to perform the following, start X (say 10) number<br/>of threads to perform some operation (ex. Copy command), I would like to<br/>exit or return out of the thread(s) that takes too long ( ex. 30 minutes<br/>). I&#39;m thinking I should spawn an independent thread (a watch thread)<br/>and have that thread watch to see if any of the 10 threads are taking<br/>longer than 30 minutes to complete. Any example how I can accomplish<br/>this? Any other suggesting would be great to. <br/><br/>BTW, I&#39;m using perl 5.8.8., thread version 1.07.<br/><br/>Thanks.<br/> http://www.nntp.perl.org/group/perl.ithreads/2011/11/msg1372.html Tue, 01 Nov 2011 23:27:31 +0000 RE: threads and alarm by Danny Wong How can you do this using perl 5.8.8?<br/><br/>-----Original Message-----<br/>From: jdhedden@gmail.com [mailto:jdhedden@gmail.com] On Behalf Of Jerry D. Hedden<br/>Sent: Thursday, September 01, 2011 5:48 AM<br/>To: alfonso caponi<br/>Cc: perl-ithreads@perl.org<br/>Subject: Re: threads and alarm<br/><br/>Signals are received by thread 0 - the main thread. Therefore, when a<br/>thread sets an alarm, the ALRM signal hits the main thread not the<br/>thread that set the alarm. I don&#39;t know your particular needs, but if<br/>you want the thread to get the alarm, then you need to set the alarm<br/>handler in the main thread, and then send the ALRM signal to the<br/>thread using $thr-&gt;kill(&#39;ALRM&#39;). Thus, in your example, if you add<br/>the following:<br/><br/>$thr-&gt;detach();<br/>$SIG{ALRM} = sub { $thr-&gt;kill(&#39;ALRM&#39;) };<br/><br/>It&#39;ll work.<br/><br/>On Thu, Sep 1, 2011 at 06:08, alfonso caponi &lt;alfonso.caponi@gmail.com&gt; wrote:<br/>&gt; Hi list,<br/>&gt;<br/>&gt; I need help! :) How can I use &quot;alarm&quot; without exit from a thread only<br/>&gt; (without making the script end!).<br/>&gt;<br/>&gt; For example this works (without threads):<br/>&gt;<br/>&gt; while(1){<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local $SIG{ALRM} = sub { print &quot;alarm!\n&quot;; };<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(2);<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;hi\n&quot;;<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep 5;<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(0);<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br/>&gt; }<br/>&gt;<br/>&gt; but the script in attachment doesn&#39;t works :(<br/>&gt;<br/>&gt; Thank you very much,<br/>&gt; Al<br/>&gt;<br/> http://www.nntp.perl.org/group/perl.ithreads/2011/09/msg1371.html Thu, 01 Sep 2011 23:05:35 +0000 Re: threads and alarm by Jerry D. Hedden The -&gt;kill() method for threads was added in threads v1.27 (back in<br/>May 2006). If you update the threads module, you&#39;ll have this<br/>capability. The &#39;best&#39; way to do this is to install Bundle::Thread<br/>from CPAN.<br/><br/>On Thu, Sep 1, 2011 at 13:27, Danny Wong (dannwong) &lt;dannwong@cisco.com&gt; wrote:<br/>&gt; How can you do this using perl 5.8.8?<br/>&gt;<br/>&gt; -----Original Message-----<br/>&gt; From: jdhedden@gmail.com [mailto:jdhedden@gmail.com] On Behalf Of Jerry D. Hedden<br/>&gt; Sent: Thursday, September 01, 2011 5:48 AM<br/>&gt; To: alfonso caponi<br/>&gt; Cc: perl-ithreads@perl.org<br/>&gt; Subject: Re: threads and alarm<br/>&gt;<br/>&gt; Signals are received by thread 0 - the main thread. &nbsp;Therefore, when a<br/>&gt; thread sets an alarm, the ALRM signal hits the main thread not the<br/>&gt; thread that set the alarm. &nbsp;I don&#39;t know your particular needs, but if<br/>&gt; you want the thread to get the alarm, then you need to set the alarm<br/>&gt; handler in the main thread, and then send the ALRM signal to the<br/>&gt; thread using $thr-&gt;kill(&#39;ALRM&#39;). &nbsp;Thus, in your example, if you add<br/>&gt; the following:<br/>&gt;<br/>&gt; $thr-&gt;detach();<br/>&gt; $SIG{ALRM} = sub { $thr-&gt;kill(&#39;ALRM&#39;) };<br/>&gt;<br/>&gt; It&#39;ll work.<br/>&gt;<br/>&gt; On Thu, Sep 1, 2011 at 06:08, alfonso caponi &lt;alfonso.caponi@gmail.com&gt; wrote:<br/>&gt;&gt; Hi list,<br/>&gt;&gt;<br/>&gt;&gt; I need help! :) How can I use &quot;alarm&quot; without exit from a thread only<br/>&gt;&gt; (without making the script end!).<br/>&gt;&gt;<br/>&gt;&gt; For example this works (without threads):<br/>&gt;&gt;<br/>&gt;&gt; while(1){<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local $SIG{ALRM} = sub { print &quot;alarm!\n&quot;; };<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(2);<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;hi\n&quot;;<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep 5;<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(0);<br/>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt; but the script in attachment doesn&#39;t works :(<br/>&gt;&gt;<br/>&gt;&gt; Thank you very much,<br/>&gt;&gt; Al<br/>&gt;&gt;<br/>&gt;<br/> http://www.nntp.perl.org/group/perl.ithreads/2011/09/msg1370.html Thu, 01 Sep 2011 10:54:13 +0000 Re: threads and alarm by Jerry D. Hedden Signals are received by thread 0 - the main thread. Therefore, when a<br/>thread sets an alarm, the ALRM signal hits the main thread not the<br/>thread that set the alarm. I don&#39;t know your particular needs, but if<br/>you want the thread to get the alarm, then you need to set the alarm<br/>handler in the main thread, and then send the ALRM signal to the<br/>thread using $thr-&gt;kill(&#39;ALRM&#39;). Thus, in your example, if you add<br/>the following:<br/><br/>$thr-&gt;detach();<br/>$SIG{ALRM} = sub { $thr-&gt;kill(&#39;ALRM&#39;) };<br/><br/>It&#39;ll work.<br/><br/>On Thu, Sep 1, 2011 at 06:08, alfonso caponi &lt;alfonso.caponi@gmail.com&gt; wrote:<br/>&gt; Hi list,<br/>&gt;<br/>&gt; I need help! :) How can I use &quot;alarm&quot; without exit from a thread only<br/>&gt; (without making the script end!).<br/>&gt;<br/>&gt; For example this works (without threads):<br/>&gt;<br/>&gt; while(1){<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local $SIG{ALRM} = sub { print &quot;alarm!\n&quot;; };<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(2);<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;hi\n&quot;;<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep 5;<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alarm(0);<br/>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br/>&gt; }<br/>&gt;<br/>&gt; but the script in attachment doesn&#39;t works :(<br/>&gt;<br/>&gt; Thank you very much,<br/>&gt; Al<br/>&gt;<br/> http://www.nntp.perl.org/group/perl.ithreads/2011/09/msg1369.html Thu, 01 Sep 2011 05:48:19 +0000 threads and alarm by alfonso caponi #!/usr/bin/perl <br/> <br/>use strict; <br/>use warnings; <br/> <br/>use threads; <br/> <br/>$|=1; <br/> <br/>$SIG{INT} = sub { <br/> threads-&gt;exit(); <br/> exit(1); <br/>}; <br/> <br/>my $thr = threads-&gt;new(\&amp;Testsub); <br/>$thr-&gt;detach(); <br/> <br/>while(1){ <br/>} <br/> <br/>exit(0); <br/> <br/>sub Testsub{ <br/> <br/> while(1){ <br/> eval { <br/> <br/> local $SIG{ALRM} = sub { print &quot;alarm!\n&quot;; }; <br/> <br/> alarm(2); <br/> <br/> sleep 5; <br/> <br/> alarm(0); <br/> }; <br/> } <br/>} <br/> http://www.nntp.perl.org/group/perl.ithreads/2011/09/msg1368.html Thu, 01 Sep 2011 03:08:36 +0000 Re: Load modules in Perl at run time and share it between threads -Stack Overflow Question by Dave Mitchell On Sat, Jul 02, 2011 at 03:45:39AM +0100, Pedro Cirne wrote:<br/>&gt; Is it possible to load modules at run time, require &#39;/file_path/file_name.pm<br/>&gt; ;&#39; , and share the memory between threads?<br/>&gt; <br/>&gt; Basically, I have a pool of threads, if thread#1 decides to load a module, I<br/>&gt; want the module available to all the threads!<br/>&gt; http://stackoverflow.com/q/6553598/758523<br/><br/>No, it&#39;s not possible.<br/>Either the module must be loaded before the thread is created, or each<br/>thread must individually load the module.<br/><br/>-- <br/>More than any other time in history, mankind faces a crossroads. One path<br/>leads to despair and utter hopelessness. The other, to total extinction.<br/>Let us pray we have the wisdom to choose correctly.<br/> -- Woody Allen<br/> http://www.nntp.perl.org/group/perl.ithreads/2011/07/msg1367.html Sat, 02 Jul 2011 01:19:47 +0000 Load modules in Perl at run time and share it between threads - StackOverflow Question by Pedro Cirne Is it possible to load modules at run time, require &#39;/file_path/file_name.pm<br/>;&#39; , and share the memory between threads?<br/><br/>Basically, I have a pool of threads, if thread#1 decides to load a module, I<br/>want the module available to all the threads!<br/>http://stackoverflow.com/q/6553598/758523<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2011/07/msg1366.html Fri, 01 Jul 2011 20:11:05 +0000 RE: Killing a thread by Danny Wong Hi Ajeet,<br/> Do you have an example you can provide? I prefer not to loop<br/>around (like sleep for 60 seconds and check again) checking for the<br/>total time and then kill the thread if it takes too long.<br/><br/>Sincerely yours,<br/>Danny H. Wong<br/><br/><br/>-----Original Message-----<br/>From: Ajeet Kumar [mailto:Ajeet.Kumar@aricent.com] <br/>Sent: Wednesday, September 29, 2010 2:34 AM<br/>To: Danny Wong (dannwong); perl-ithreads@perl.org<br/>Subject: RE: Killing a thread<br/><br/>Hello Wong,<br/><br/>Following is my approach in this case...<br/><br/>--Execute the &quot;ps -A&quot; command<br/>-- Observe the third column , which is total time taken by process<br/>-- check the process ID from the first column<br/>-- send a kill -9 signal to required process ID<br/><br/>If any confusion is left, then do mail back...<br/><br/>-----Original Message-----<br/>From: Danny Wong (dannwong) [mailto:dannwong@cisco.com]<br/>Sent: Wednesday, September 29, 2010 4:12 AM<br/>To: perl-ithreads@perl.org<br/>Subject: Killing a thread<br/><br/>Hi Perl Thread GURUS,<br/> Here is my situation. I started, say 10 threads, executing some<br/>command, 1 or 2 of them is hanging for over 2 hours. I want to put in<br/>some logic that if the thread run time passes an hour threshold kill<br/>that thread or return and fail the thread. Any ideas on how I can<br/>accomplish this task? Thanks.<br/><br/>Sincerely yours,<br/>Danny H. Wong<br/><br/><br/><br/>&quot;DISCLAIMER: This message is proprietary to Aricent and is intended<br/>solely for the use of the individual to whom it is addressed. It may<br/>contain privileged or confidential information and should not be<br/>circulated or used for any purpose other than for what it is intended.<br/>If you have received this message in error, please notify the originator<br/>immediately. If you are not the intended recipient, you are notified<br/>that you are strictly prohibited from using, copying, altering, or<br/>disclosing the contents of this message. Aricent accepts no<br/>responsibility for loss or damage arising from the use of the<br/>information transmitted by this email including damage from virus.&quot;<br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1365.html Thu, 30 Sep 2010 01:00:48 +0000 Re: Killing a thread by Jerry D. Hedden To my knowledge, if the thread hangs on a system call, then you&#39;ll<br/>need to do something similar to what was suggested by the previous<br/>poster.<br/><br/>On Wed, Sep 29, 2010 at 10:47, Danny Wong (dannwong) &lt;dannwong@cisco.com&gt; wrote:<br/>&gt; Hi Jerry,<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;Is there an example you or someone can provide, if the thread is<br/>&gt; hanging, I would just like the thread to &quot;return failure or timeout&quot; and<br/>&gt; ignore the system call. Let the system call hang and I&#39;ll clean it up<br/>&gt; afterwards at the very end of the script.<br/>&gt;<br/>&gt; Sincerely yours,<br/>&gt; Danny H. Wong<br/>&gt;<br/>&gt; -----Original Message-----<br/>&gt; From: jdhedden@gmail.com [mailto:jdhedden@gmail.com] On Behalf Of Jerry<br/>&gt; D. Hedden<br/>&gt; Sent: Wednesday, September 29, 2010 5:47 AM<br/>&gt; To: Danny Wong (dannwong)<br/>&gt; Cc: perl-ithreads@perl.org<br/>&gt; Subject: Re: Killing a thread<br/>&gt;<br/>&gt; Danny Wong wrote:<br/>&gt;&gt; Here is my situation. I started, say 10 threads, executing<br/>&gt;&gt; some command, 1 or 2 of them is hanging for over 2 hours.<br/>&gt;&gt; I want to put in some logic that if the thread run time<br/>&gt;&gt; passes an hour threshold kill that thread or return and<br/>&gt;&gt; fail the thread. Any ideas on how I can accomplish this<br/>&gt;&gt; task? Thanks.<br/>&gt;<br/>&gt; This problem is addressed in the &#39;examples/pool.pl&#39; file in<br/>&gt; the &#39;threads&#39; distribution on CPAN. &nbsp;From the POD (note the<br/>&gt; third bullet):<br/>&gt;<br/>&gt; &nbsp; &nbsp;NAME<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;pool.pl - Simple &#39;threads&#39; example<br/>&gt;<br/>&gt; &nbsp; &nbsp;DESCRIPTION<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;A simplistic example illustrating the following:<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; Management of a pool of threads<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; Communication between threads using queues<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; Timing out and cancelling threads<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; Interrupting a threaded program<br/>&gt; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; Cleaning up threads before terminating<br/>&gt;<br/>&gt; However, if the &#39;hung&#39; threads are stuck on some system<br/>&gt; call, then the above will not work because of Perl&#39;s &quot;safe<br/>&gt; signals&quot;. &nbsp;You&#39;ll need to read the section called &quot;Deferred<br/>&gt; Signals (Safe Signals)&quot; in &quot;perldoc perlipc&quot;.<br/>&gt;<br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1364.html Wed, 29 Sep 2010 08:11:59 +0000 RE: Killing a thread by Danny Wong Hi Jerry,<br/> Is there an example you or someone can provide, if the thread is<br/>hanging, I would just like the thread to &quot;return failure or timeout&quot; and<br/>ignore the system call. Let the system call hang and I&#39;ll clean it up<br/>afterwards at the very end of the script.<br/><br/>Sincerely yours,<br/>Danny H. Wong<br/><br/>-----Original Message-----<br/>From: jdhedden@gmail.com [mailto:jdhedden@gmail.com] On Behalf Of Jerry<br/>D. Hedden<br/>Sent: Wednesday, September 29, 2010 5:47 AM<br/>To: Danny Wong (dannwong)<br/>Cc: perl-ithreads@perl.org<br/>Subject: Re: Killing a thread<br/><br/>Danny Wong wrote:<br/>&gt; Here is my situation. I started, say 10 threads, executing<br/>&gt; some command, 1 or 2 of them is hanging for over 2 hours.<br/>&gt; I want to put in some logic that if the thread run time<br/>&gt; passes an hour threshold kill that thread or return and<br/>&gt; fail the thread. Any ideas on how I can accomplish this<br/>&gt; task? Thanks.<br/><br/>This problem is addressed in the &#39;examples/pool.pl&#39; file in<br/>the &#39;threads&#39; distribution on CPAN. From the POD (note the<br/>third bullet):<br/><br/> NAME<br/> pool.pl - Simple &#39;threads&#39; example<br/><br/> DESCRIPTION<br/> A simplistic example illustrating the following:<br/> * Management of a pool of threads<br/> * Communication between threads using queues<br/> * Timing out and cancelling threads<br/> * Interrupting a threaded program<br/> * Cleaning up threads before terminating<br/><br/>However, if the &#39;hung&#39; threads are stuck on some system<br/>call, then the above will not work because of Perl&#39;s &quot;safe<br/>signals&quot;. You&#39;ll need to read the section called &quot;Deferred<br/>Signals (Safe Signals)&quot; in &quot;perldoc perlipc&quot;.<br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1363.html Wed, 29 Sep 2010 08:02:05 +0000 Re: Killing a thread by Jerry D. Hedden Danny Wong wrote:<br/>&gt; Here is my situation. I started, say 10 threads, executing<br/>&gt; some command, 1 or 2 of them is hanging for over 2 hours.<br/>&gt; I want to put in some logic that if the thread run time<br/>&gt; passes an hour threshold kill that thread or return and<br/>&gt; fail the thread. Any ideas on how I can accomplish this<br/>&gt; task? Thanks.<br/><br/>This problem is addressed in the &#39;examples/pool.pl&#39; file in<br/>the &#39;threads&#39; distribution on CPAN. From the POD (note the<br/>third bullet):<br/><br/> NAME<br/> pool.pl - Simple &#39;threads&#39; example<br/><br/> DESCRIPTION<br/> A simplistic example illustrating the following:<br/> * Management of a pool of threads<br/> * Communication between threads using queues<br/> * Timing out and cancelling threads<br/> * Interrupting a threaded program<br/> * Cleaning up threads before terminating<br/><br/>However, if the &#39;hung&#39; threads are stuck on some system<br/>call, then the above will not work because of Perl&#39;s &quot;safe<br/>signals&quot;. You&#39;ll need to read the section called &quot;Deferred<br/>Signals (Safe Signals)&quot; in &quot;perldoc perlipc&quot;.<br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1362.html Wed, 29 Sep 2010 05:48:04 +0000 RE: Killing a thread by Ajeet Kumar Hello Wong,<br/><br/>Following is my approach in this case...<br/><br/>--Execute the &quot;ps -A&quot; command<br/>-- Observe the third column , which is total time taken by process<br/>-- check the process ID from the first column<br/>-- send a kill -9 signal to required process ID<br/><br/>If any confusion is left, then do mail back...<br/><br/>-----Original Message-----<br/>From: Danny Wong (dannwong) [mailto:dannwong@cisco.com]<br/>Sent: Wednesday, September 29, 2010 4:12 AM<br/>To: perl-ithreads@perl.org<br/>Subject: Killing a thread<br/><br/>Hi Perl Thread GURUS,<br/> Here is my situation. I started, say 10 threads, executing some<br/>command, 1 or 2 of them is hanging for over 2 hours. I want to put in<br/>some logic that if the thread run time passes an hour threshold kill<br/>that thread or return and fail the thread. Any ideas on how I can<br/>accomplish this task? Thanks.<br/><br/>Sincerely yours,<br/>Danny H. Wong<br/><br/><br/><br/>&quot;DISCLAIMER: This message is proprietary to Aricent and is intended solely for the use of the individual to whom it is addressed. It may contain privileged or confidential information and should not be circulated or used for any purpose other than for what it is intended. If you have received this message in error, please notify the originator immediately. If you are not the intended recipient, you are notified that you are strictly prohibited from using, copying, altering, or disclosing the contents of this message. Aricent accepts no responsibility for loss or damage arising from the use of the information transmitted by this email including damage from virus.&quot;<br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1361.html Wed, 29 Sep 2010 02:34:01 +0000 Killing a thread by Danny Wong Hi Perl Thread GURUS,<br/> Here is my situation. I started, say 10 threads, executing some<br/>command, 1 or 2 of them is hanging for over 2 hours. I want to put in<br/>some logic that if the thread run time passes an hour threshold kill<br/>that thread or return and fail the thread. Any ideas on how I can<br/>accomplish this task? Thanks.<br/><br/>Sincerely yours,<br/>Danny H. Wong<br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2010/09/msg1360.html Wed, 29 Sep 2010 02:20:54 +0000 Re: Help with concurrent threads by Jerry D. Hedden I tweaked it a bit. Attached.<br/><br/>On Thu, May 6, 2010 at 06:02, alfonso caponi &lt;alfonso.caponi@gmail.com&gt; wrote:<br/>&gt; Inspired by the pool.pl example, I wrote a simple multi-thread script for<br/>&gt; dns server testing on my LAN. It seems working fine, but I would like your<br/>&gt; opinion on any improvements.<br/>&gt;<br/>&gt; Note: I&#39;ve commented &quot;#while (! $TERM)&quot; to avoid multiple requests while !<br/>&gt; $TERM :(<br/>&gt;<br/>&gt; Thank you very much at all!<br/>&gt;<br/>&gt; 2010/4/16 Jerry D. Hedden &lt;jdhedden@cpan.org&gt;<br/>&gt;&gt;<br/>&gt;&gt; Get the &#39;threads&#39; distribution from CPAN and look at the sample<br/>&gt;&gt; scripts in the &#39;examples&#39; directory.<br/>&gt;&gt;<br/>&gt;&gt; On Fri, Apr 16, 2010 at 11:48, alfonso caponi &lt;alfonso.caponi@gmail.com&gt;<br/>&gt;&gt; wrote:<br/>&gt;&gt; &gt; Hi list,<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; I&#39;ve some doubts with a multi-threads script. My goal is to create a<br/>&gt;&gt; &gt; script<br/>&gt;&gt; &gt; with the same max number of threads running at all times.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; For example: I&#39;ve an array with 9 elements and 3 threads to process it;<br/>&gt;&gt; &gt; each<br/>&gt;&gt; &gt; thread can process an element using a different time (a thread could end<br/>&gt;&gt; &gt; before another) and I would like 3 threads always running.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; My idea is to take an element from a queue, start threads to reach the<br/>&gt;&gt; &gt; maximum number of active threads allowed and simultaneously start a<br/>&gt;&gt; &gt; separate<br/>&gt;&gt; &gt; (but unique) thread to wait every thread in execution.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; In attachment my &quot;solution&quot;. Have you any tips? I would not use other<br/>&gt;&gt; &gt; modules as well threads, threads::shared, thread::queue.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; Thank you very much!<br/>&gt;&gt; &gt; Al<br/>&gt;<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2010/05/msg1359.html Thu, 06 May 2010 06:26:40 +0000 Re: Help with concurrent threads by alfonso caponi #!/usr/bin/perl <br/> <br/>$|=1; <br/> <br/>use strict; <br/>use warnings; <br/> <br/>use Net::DNS; <br/>use threads; <br/>use threads::shared; <br/>use Thread::Queue; <br/>use Uniq; <br/> <br/>my $ip_list = $ARGV[0]; <br/> <br/>die &quot;$0 &lt;ip list file&gt;\n&quot; unless $ARGV[0]; <br/> <br/>######################## <br/>### Global Variables ### <br/> <br/># FQDN list <br/>my @fqdn = (&quot;www.nba.com&quot;,&quot;www.google.com&quot;,&quot;www.kernel.org&quot;,&quot;www.linux.org&quot;); <br/> <br/># Maximum working threads <br/>my $MAX_THREADS = 10; <br/> <br/># Maximum thread working time <br/>my $TIMEOUT = 10; <br/> <br/># Flag to inform all threads that application is terminating <br/>my $TERM :shared = 0; <br/> <br/># Prevents double detach attempts <br/>my $DETACHING :shared; <br/> <br/>####################### <br/>### Signal Handling ### <br/> <br/># Gracefully terminate application on ^C or command line &#39;kill&#39; <br/>$SIG{&#39;INT&#39;} = $SIG{&#39;TERM&#39;} = sub { <br/> print(&quot;&gt;&gt;&gt; Terminating &lt;&lt;&lt;\n&quot;); <br/> $TERM = 1; <br/>}; <br/> <br/># This signal handler is called inside threads <br/># that get cancelled by the timer thread <br/>$SIG{&#39;KILL&#39;} = sub { <br/> # Tell user we&#39;ve been terminated <br/> printf(&quot;%3d &lt;- Killed\n&quot;, threads-&gt;tid()); <br/> # Detach and terminate <br/> lock($DETACHING); <br/> threads-&gt;detach() if ! threads-&gt;is_detached(); <br/> threads-&gt;exit(); <br/>}; <br/> <br/>##### <br/> <br/>my @hosts; <br/>open(IPLIST, &quot;&lt;$ip_list&quot;) || die &quot;Error : cannot open the ip address list file: $!\n&quot;; <br/> chomp (@hosts = &lt;IPLIST&gt;); <br/>close (IPLIST); <br/> <br/>@hosts = uniq sort @hosts; <br/> <br/>### fill the queue <br/>our $q :shared; <br/>$q = new Thread::Queue; <br/>foreach my $host (@hosts) { <br/> foreach my $fqdn (@fqdn) { <br/> $q-&gt;enqueue(&quot;$host;$fqdn&quot;); <br/> } <br/>} <br/> <br/>############################### <br/>### Main Processing Section ### <br/> <br/>MAIN: { <br/> <br/> # Start timer thread <br/> print &quot;[+] Start timer thread\n&quot;; <br/> my $queue = Thread::Queue-&gt;new(); <br/> threads-&gt;create(&#39;Timer&#39;,$queue)-&gt;detach(); <br/> <br/> # Manage the thread pool until signalled to terminate <br/> while (! $TERM) { <br/> <br/> # Keep max threads running <br/> for (my $needed = $MAX_THREADS - threads-&gt;list(); $needed &amp;&amp; ! $TERM; $needed--) { <br/> <br/> my $element = $q-&gt;dequeue_nb; <br/> <br/> # New thread <br/> threads-&gt;create(&#39;RequestDNS&#39;,$queue,$element,$TIMEOUT); <br/> } <br/> <br/> # Wait for any threads to finish <br/> sleep(1); <br/> } <br/> <br/> # Wait for max timeout for threads to finish <br/> while ((threads-&gt;list() &gt; 0) &amp;&amp; $TIMEOUT--) { <br/> sleep(1); <br/> } <br/> <br/> # Detach and kill any remaining threads <br/> foreach my $thr (threads-&gt;list()) { <br/> lock($DETACHING); <br/> $thr-&gt;detach() if ! $thr-&gt;is_detached(); <br/> $thr-&gt;kill(&#39;KILL&#39;); <br/> } <br/>} <br/> <br/>exit(0); <br/> <br/>sub RequestDNS { <br/> <br/> my ($queue,$element,$timeout) = @_; <br/> <br/> my ($dns_server,$fqdn) = split(/;/,$element); <br/> <br/> # My thread ID <br/> my $tid = threads-&gt;tid(); <br/> printf(&quot;$dns_server -&gt; %3d\n&quot;, $tid); <br/> <br/> # Register with timer thread <br/> $queue-&gt;enqueue($tid,$timeout); <br/> <br/> # Do some work while monitoring $TERM <br/> #while (! $TERM) { <br/> <br/> print &quot;try $dns_server;$fqdn\n&quot;; <br/> <br/> my $res = Net::DNS::Resolver-&gt;new; <br/> $res-&gt;nameservers($dns_server); <br/> <br/> my $query = $res-&gt;query($fqdn,&quot;A&quot;); <br/> <br/> if ($query) { <br/> foreach my $rr (grep { $_-&gt;type eq &#39;A&#39; } $query-&gt;answer) { <br/> my $reply = $rr-&gt;address; <br/> print &quot;$dns_server;$fqdn;$reply\n&quot;; <br/> } <br/> }# else { <br/> # warn &quot;query failed: $dns_server ($fqdn)&quot;, $res-&gt;errorstring, &quot;\n&quot;; <br/> #} <br/> #} <br/> <br/> # Remove signal handler <br/> $SIG{&#39;KILL&#39;} = sub {}; <br/> <br/> # Unregister with timer thread <br/> $queue-&gt;enqueue($tid, undef); <br/> <br/> # Tell user we&#39;re done <br/> printf(&quot;%3d &lt;- Finished\n&quot;, $tid); <br/> <br/> # Detach and terminate <br/> lock($DETACHING); <br/> threads-&gt;detach() if ! threads-&gt;is_detached(); <br/> threads-&gt;exit(); <br/>} <br/> <br/># The timer thread that monitors other threads for timeout <br/>sub Timer { <br/> <br/> my $queue = shift; # The registration queue <br/> my %timers; # Contains threads and timeouts <br/> <br/> # Loop until told to quit <br/> while (! $TERM) { <br/> <br/> # Check queue <br/> while (my $tid = $queue-&gt;dequeue_nb()) { <br/> if (! ($timers{$tid}{&#39;timeout&#39;} = $queue-&gt;dequeue()) || ! ($timers{$tid}{&#39;thread&#39;} = threads-&gt;object($tid))) { <br/> # No timeout - unregister thread <br/> delete($timers{$tid}); <br/> } <br/> } <br/> <br/> # Cancel timed out threads <br/> foreach my $tid (keys(%timers)) { <br/> if (--$timers{$tid}{&#39;timeout&#39;} &lt; 0) { <br/> $timers{$tid}{&#39;thread&#39;}-&gt;kill(&#39;KILL&#39;); <br/> delete($timers{$tid}); <br/> } <br/> } <br/> <br/> # Tick tock <br/> sleep(1); <br/> } <br/>} <br/> http://www.nntp.perl.org/group/perl.ithreads/2010/05/msg1358.html Thu, 06 May 2010 03:02:49 +0000