perl.ithreads http://www.nntp.perl.org/group/perl.ithreads/ ... Copyright 1998-2008 perl.org Tue, 07 Oct 2008 23:57:02 +0000 ask@perl.org Re: Lots of context switches with shared array by Dean Arnold Thomas Karcher wrote:<br/>&gt; Hi Dean,<br/>&gt; <br/>&gt; <br/>&gt; If someone from the ithreads development team is listening: I guess this<br/>&gt; is an issue worth considering to have a solution for ... e. g. a<br/>&gt; thread-safe array (similar to Thread::Queue) or a more fine-granular<br/>&gt; locking mechanism for shared arrays ... not that I really thought it<br/>&gt; through :)<br/>&gt; <br/><br/>FWIW: Thread::Queue *is* a thread::shared array, w/ just a few methods<br/>added. And it suffers from the same issues as your sort-merge, assuming<br/>enough threads are accessing the queue concurrently.<br/><br/>The core issue is the global lock on the shared interpretter; until thats removed<br/>(which doesn&#39;t seem likely in the near future), shared data will remain<br/>a &quot;contentious&quot; issue.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1327.html Tue, 23 Sep 2008 12:18:58 +0000 Re: Lots of context switches with shared array by Thomas Karcher Hi Dean,<br/><br/>&gt; &gt; Thanks for the enlightening answer. Do you think it is possible to<br/>&gt; &gt; &quot;trick&quot; threads::shared&#39;s lock with a reference to the array, i. e. the<br/>&gt; &gt; reference is shared but the array itself isn&#39;t?<br/>&gt; Not certain how you intend to do that ? Are you intending to write your<br/>&gt; own XS implementation ? Keep in mind that a shared ref must point to<br/><br/>No, that&#39;s too much and not an option for me.<br/><br/>&gt; a shared variable, ie, a shared variable can&#39;t be used as a ref to<br/>&gt; a private variable.<br/><br/>Right, that kills the idea ... too bad. Thanks for your comments.<br/><br/><br/>If someone from the ithreads development team is listening: I guess this<br/>is an issue worth considering to have a solution for ... e. g. a<br/>thread-safe array (similar to Thread::Queue) or a more fine-granular<br/>locking mechanism for shared arrays ... not that I really thought it<br/>through :)<br/><br/><br/>Regards,<br/>Thomas<br/><br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1326.html Tue, 23 Sep 2008 10:42:46 +0000 Re: Lots of context switches with shared array by Dean Arnold Thomas Karcher wrote:<br/>&gt; Hi Dean,<br/>&gt; <br/>&gt; <br/>&gt; Thanks for the enlightening answer. Do you think it is possible to<br/>&gt; &quot;trick&quot; threads::shared&#39;s lock with a reference to the array, i. e. the<br/>&gt; reference is shared but the array itself isn&#39;t?<br/>&gt; <br/><br/>Not certain how you intend to do that ? Are you intending to write your<br/>own XS implementation ? Keep in mind that a shared ref must point to<br/>a shared variable, ie, a shared variable can&#39;t be used as a ref to<br/>a private variable.<br/><br/>Dean Arnold<br/>Presicient Corp.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1325.html Tue, 23 Sep 2008 10:33:27 +0000 Re: Lots of context switches with shared array by Thomas Karcher Hi Dean,<br/><br/>&gt; &gt; I&#39;m writing a multithreaded merge sort that suffers from massive amounts<br/>&gt; &gt; of context switches (as seen by vmstat). The outline of the program is<br/>&gt; &gt; roughly as follows:<br/>&gt; &gt; <br/>&gt; &gt; ====<br/>&gt; &gt; my @sortdata : shared; // the values to be sorted<br/>&gt; &gt; [...]<br/>&gt; &gt; // starting two threads<br/>&gt; &gt; new threads( \&amp;mergesort_string, $begin, $half, $threaddepth - 1 );<br/>&gt; &gt; new threads( \&amp;mergesort_string, $half, $end, $threaddepth - 1 );<br/>&gt; &gt; [...]<br/>&gt; &gt; <br/>&gt; &gt; sub mergesort_string {<br/>&gt; &gt; my ( $begin, $end, $threaddepth ) = @_;<br/>&gt; &gt; <br/>&gt; &gt; // sorting &quot;my&quot; part of @sortdata, as delimited<br/>&gt; &gt; // by $begin and $end<br/>&gt; &gt; }<br/>&gt; &gt; ====<br/>&gt; &gt; <br/>&gt; &gt; By playing around, I discovered that the &quot;shared&quot; is what causes the<br/>&gt; &gt; context switches. They take up about 40% of the duration of the program<br/>&gt; &gt; for an array of 10,000 elements.<br/>&gt; &gt; <br/>&gt; &gt; I don&#39;t need any locking on the elements itself because the merge sort<br/>&gt; &gt; algorithm itself guarantees in my opinion that there is no conflicting<br/>&gt; &gt; access to them.<br/>&gt; &gt; <br/>&gt; &gt; Am I missing something here? How can I avoid those context switches that<br/>&gt; &gt; make the program essentially useless?<br/>&gt; I&#39;m afraid there isn&#39;t much you can do short of writing your own XS code that<br/>&gt; bypasses threads::shared. Due to the global lock on the shared interpretter (where<br/>&gt; shared variables are actually stored), each thread that touches the array is going<br/>&gt; to be trying to acquire the global lock, ergo lots of lock contention, leading to lots<br/>&gt; of probable context switches.<br/>&gt; You may be able to improve performance by copying parts of the array out to a private<br/>&gt; version, but that may not adapt to your algorithm.<br/><br/>Thanks for the enlightening answer. Do you think it is possible to<br/>&quot;trick&quot; threads::shared&#39;s lock with a reference to the array, i. e. the<br/>reference is shared but the array itself isn&#39;t?<br/><br/><br/>Thanks,<br/>Thomas<br/><br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1324.html Tue, 23 Sep 2008 08:32:54 +0000 Re: Lots of context switches with shared array by Dean Arnold Thomas Karcher wrote:<br/>&gt; Hi there,<br/>&gt; <br/>&gt; I&#39;m writing a multithreaded merge sort that suffers from massive amounts<br/>&gt; of context switches (as seen by vmstat). The outline of the program is<br/>&gt; roughly as follows:<br/>&gt; <br/>&gt; ====<br/>&gt; my @sortdata : shared; // the values to be sorted<br/>&gt; [...]<br/>&gt; // starting two threads<br/>&gt; new threads( \&amp;mergesort_string, $begin, $half, $threaddepth - 1 );<br/>&gt; new threads( \&amp;mergesort_string, $half, $end, $threaddepth - 1 );<br/>&gt; [...]<br/>&gt; <br/>&gt; sub mergesort_string {<br/>&gt; my ( $begin, $end, $threaddepth ) = @_;<br/>&gt; <br/>&gt; // sorting &quot;my&quot; part of @sortdata, as delimited<br/>&gt; // by $begin and $end<br/>&gt; }<br/>&gt; ====<br/>&gt; <br/>&gt; By playing around, I discovered that the &quot;shared&quot; is what causes the<br/>&gt; context switches. They take up about 40% of the duration of the program<br/>&gt; for an array of 10,000 elements.<br/>&gt; <br/>&gt; I don&#39;t need any locking on the elements itself because the merge sort<br/>&gt; algorithm itself guarantees in my opinion that there is no conflicting<br/>&gt; access to them.<br/>&gt; <br/>&gt; Am I missing something here? How can I avoid those context switches that<br/>&gt; make the program essentially useless?<br/>&gt; <br/><br/>I&#39;m afraid there isn&#39;t much you can do short of writing your own XS code that<br/>bypasses threads::shared. Due to the global lock on the shared interpretter (where<br/>shared variables are actually stored), each thread that touches the array is going<br/>to be trying to acquire the global lock, ergo lots of lock contention, leading to lots<br/>of probable context switches.<br/><br/>You may be able to improve performance by copying parts of the array out to a private<br/>version, but that may not adapt to your algorithm.<br/><br/>Dean Arnold<br/>Presicient Corp.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1323.html Tue, 23 Sep 2008 08:21:11 +0000 Lots of context switches with shared array by Thomas Karcher Hi there,<br/><br/>I&#39;m writing a multithreaded merge sort that suffers from massive amounts<br/>of context switches (as seen by vmstat). The outline of the program is<br/>roughly as follows:<br/><br/>====<br/>my @sortdata : shared; // the values to be sorted<br/>[...]<br/>// starting two threads<br/>new threads( \&amp;mergesort_string, $begin, $half, $threaddepth - 1 );<br/>new threads( \&amp;mergesort_string, $half, $end, $threaddepth - 1 );<br/>[...]<br/><br/>sub mergesort_string {<br/> my ( $begin, $end, $threaddepth ) = @_;<br/><br/> // sorting &quot;my&quot; part of @sortdata, as delimited<br/> // by $begin and $end<br/>}<br/>====<br/><br/>By playing around, I discovered that the &quot;shared&quot; is what causes the<br/>context switches. They take up about 40% of the duration of the program<br/>for an array of 10,000 elements.<br/><br/>I don&#39;t need any locking on the elements itself because the merge sort<br/>algorithm itself guarantees in my opinion that there is no conflicting<br/>access to them.<br/><br/>Am I missing something here? How can I avoid those context switches that<br/>make the program essentially useless?<br/><br/><br/>Thanks!<br/><br/>Thomas<br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/09/msg1322.html Tue, 23 Sep 2008 08:10:31 +0000 Re: XS callbacks from different threads. by Josef Wolf On Thu, Aug 07, 2008 at 10:02:29PM +0300, Shmuel Fomberg wrote:<br/><br/>&gt; &gt;&gt;&gt;static SV *callback_ref = (SV*)NULL;<br/>&gt; &gt;&gt;Reality check: you do know that &quot;static&quot; means here, right?<br/>&gt; &gt;I think so: I have exactly one instance of this variable. Do you see<br/>&gt; &gt;any problems with this?<br/>&gt; <br/>&gt; Um, you are thinking C++.<br/><br/>No, I&#39;m thinking pure C here. I don&#39;t line C++ very much. (please<br/>feel free to check http://yosefk.com/c++fqa/ for some reasons why<br/>I don&#39;t like C++)<br/><br/>&gt; declaring static variable outside object or <br/>&gt; function says something about it&#39;s scope. (you still get one instance of <br/>&gt; this, even without the static)<br/><br/>Since we were talking about threads, I assumed that your question was<br/>about having one instance (shared by all threads) against having one<br/>instance per thread. I need to pass the callback reference from the<br/>main thread to the callback thread, so having a shared variable for<br/>this is exactly what I want.<br/><br/>The scope should surely be limited to my file, because I don&#39;t want to<br/>export objects without a reason. I still don&#39;t see any problem with<br/>this.<br/><br/>&gt; &gt;&gt;&gt;int call_perl (int cnt, ...)<br/>&gt; &gt;&gt;&gt;{<br/>&gt; &gt;&gt;&gt; dTHX;<br/>&gt; &gt;&gt;&gt; PERL_SET_CONTEXT(my_perl);<br/>&gt; &gt;&gt;I hate the &quot;my_perl&quot; parameter. Who defined it? what is it set to? <br/>&gt; &gt;AFAIK, this is created by dTHX.<br/>&gt; <br/>&gt; Correct. but from where does the dTHX take it? from the connection <br/>&gt; between the thread and one perl interpreter. who makes the connection? <br/>&gt; PERL_SET_CONTEXT. but the PERL_SET_CONTEXT is inited with my_perl... see <br/>&gt; the loop here?<br/><br/>I agree that the dTHXa() method you mentioned is the cleaner method.<br/>BTW: I can&#39;t find any reference of this macro in the documentation. Are<br/>you sure this is officially part of the API?<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1321.html Fri, 08 Aug 2008 08:10:29 +0000 Re: XS callbacks from different threads. by Shmuel Fomberg Josef Wolf wrote:<br/><br/>&gt;&gt; Well, maybe, but there is already one thread that is passing through <br/>&gt;&gt; perl, so calling PERL_SET_CONTEXT for this perl on other thread is <br/>&gt;&gt; pulling the rug under the first thread&#39;s legs. it can&#39;t be good.<br/>&gt; I&#39;ve got it working now. The solution is to PERL_SET_CONTEXT()<br/>&gt; _before_ declaring the context with dTHX. The code looks like this now:<br/><br/>Interesting to know that it&#39;s working. Thanks.<br/><br/>&gt;&gt;&gt; static SV *callback_ref = (SV*)NULL;<br/>&gt;&gt; Reality check: you do know that &quot;static&quot; means here, right?<br/>&gt; I think so: I have exactly one instance of this variable. Do you see<br/>&gt; any problems with this?<br/><br/>Um, you are thinking C++. declaring static variable outside object or <br/>function says something about it&#39;s scope. (you still get one instance of <br/>this, even without the static)<br/><br/>&gt;&gt;&gt; int call_perl (int cnt, ...)<br/>&gt;&gt;&gt; {<br/>&gt;&gt;&gt; dTHX;<br/>&gt;&gt;&gt; PERL_SET_CONTEXT(my_perl);<br/>&gt;&gt; I hate the &quot;my_perl&quot; parameter. Who defined it? what is it set to? <br/>&gt; AFAIK, this is created by dTHX.<br/><br/>Correct. but from where does the dTHX take it? from the connection <br/>between the thread and one perl interpreter. who makes the connection? <br/>PERL_SET_CONTEXT. but the PERL_SET_CONTEXT is inited with my_perl... see <br/>the loop here?<br/><br/>Shmuel.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1320.html Thu, 07 Aug 2008 12:04:26 +0000 Re: XS callbacks from different threads. by Josef Wolf Thanks for your help and your passience, Shmuel!<br/><br/>On Tue, Aug 05, 2008 at 11:03:58PM +0300, Shmuel Fomberg wrote:<br/><br/>&gt; Well, maybe, but there is already one thread that is passing through <br/>&gt; perl, so calling PERL_SET_CONTEXT for this perl on other thread is <br/>&gt; pulling the rug under the first thread&#39;s legs. it can&#39;t be good.<br/><br/>I&#39;ve got it working now. The solution is to PERL_SET_CONTEXT()<br/>_before_ declaring the context with dTHX. The code looks like this now:<br/><br/><br/>static PerlInterpreter *orig_perl;<br/>static SV *callback_ref = (SV*)NULL;<br/><br/>static int do_call_perl (size_t argc, char *argv[])<br/>{<br/> dTHX;<br/> dSP;<br/> I32 ax;<br/> /* ... snipped here ... */<br/>}<br/><br/>static int call_perl_argv (size_t argc, char *argv[])<br/>{<br/> int ret;<br/> static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;<br/><br/> pthread_mutex_lock(&amp;mutex);<br/> PERL_SET_CONTEXT(orig_perl);<br/><br/> ret = do_call_perl (argc, argv);<br/><br/> pthread_mutex_unlock(&amp;mutex);<br/><br/> return ret;<br/>}<br/><br/>void register_callback (SV *callback)<br/>{<br/> dTHX;<br/><br/> if (callback_ref == (SV*)NULL) {<br/> callback_ref = newSVsv (callback); /* first time, create new SV */<br/> } else {<br/> SvSetSV (callback_ref, callback); /* been here, overwrite */<br/> }<br/><br/> orig_perl = my_perl;<br/><br/> do {<br/> sleep (3);<br/> } while (call_perl (1, &quot;check&quot;));<br/><br/> shutdown_threads ();<br/>}<br/><br/>&gt; Maybe you should use two threads: one of the calling perl, and one slave <br/>&gt; perl. because as said two paragraphs ago, I don&#39;t think that using one <br/>&gt; perl interpreter will work.<br/><br/>Now I have multiple C-threads calling one perl interpreter. Multiple<br/>entries into the interpreter are protected by a mutex. That&#39;s exactly<br/>what I&#39;ve tried to achieve (at least for now).<br/><br/>&gt; &gt;static SV *callback_ref = (SV*)NULL;<br/>&gt; <br/>&gt; Reality check: you do know that &quot;static&quot; means here, right?<br/><br/>I think so: I have exactly one instance of this variable. Do you see<br/>any problems with this?<br/><br/>&gt; &gt;int call_perl (int cnt, ...)<br/>&gt; &gt;{<br/>&gt; &gt; dTHX;<br/>&gt; &gt; PERL_SET_CONTEXT(my_perl);<br/>&gt; <br/>&gt; I hate the &quot;my_perl&quot; parameter. Who defined it? what is it set to? <br/><br/>AFAIK, this is created by dTHX.<br/><br/>&gt; Especially if you have more then one perl interpreter in you code. I used:<br/>&gt; dTHXa(ph-&gt;perl);<br/>&gt; PERL_SET_CONTEXT(ph-&gt;perl);<br/>&gt; where ph was some struct that held the perl interpreter for me...<br/><br/>Ah, I did not know about dTHXa().<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1319.html Wed, 06 Aug 2008 12:00:44 +0000 Re: XS callbacks from different threads. by Shmuel Fomberg Josef Wolf wrote:<br/><br/>&gt;&gt; For this one you need a real expert. It is possible to call into Perl <br/>&gt;&gt; from an XS sub. but to call into Perl from other threads while it is <br/>&gt;&gt; inside a XS call? I have no idea.<br/>&gt; <br/>&gt; As far as I understand the corresponding section in perlguts, it should<br/>&gt; be possible _if_ PERL_SET_CONTEXT() is called before. But for some<br/>&gt; reason it keeps crashing :-(<br/><br/>Well, maybe, but there is already one thread that is passing through <br/>perl, so calling PERL_SET_CONTEXT for this perl on other thread is <br/>pulling the rug under the first thread&#39;s legs. it can&#39;t be good.<br/><br/>&gt;&gt;&gt; 2. I need to create a second perl interpreter (or clone the first one).<br/>&gt;&gt;&gt; Can somebody point me to example code how to do that?<br/>&gt;&gt; That&#39;s actually no big deal. look at perldoc page perlembed, there are <br/>&gt;&gt; examples.<br/>&gt; What I am missing from those examples is the explanation how the<br/>&gt; different interpreters would communicate.<br/><br/>There are two ways to communicate between two thread: using Perl&#39;s <br/>mechanizem, or using C. either possible. just stick to one and don&#39;t mix.<br/>For example, if you choose to use C communication, then after initizing <br/>the lib, the master perl should call some C function that waits until <br/>the lib finished working / need restart / etc. The lib will use the same <br/>system to release the master from it&#39;s sleep, either directly or even an <br/>XS sub that the perl code will call. (C calling Perl calling C...)<br/><br/>&gt; And there&#39;s one more complication: the low-level library don&#39;t even<br/>&gt; state how many threads will call my callback. Therefore I would<br/>&gt; prefer to have only _one_ interpreter and use a mutex to keep the<br/>&gt; threads from entering the interpreter simultaneously.<br/><br/>Maybe you should use two threads: one of the calling perl, and one slave <br/>perl. because as said two paragraphs ago, I don&#39;t think that using one <br/>perl interpreter will work.<br/><br/>&gt; static SV *callback_ref = (SV*)NULL;<br/><br/>Reality check: you do know that &quot;static&quot; means here, right?<br/><br/>&gt; int call_perl (int cnt, ...)<br/>&gt; {<br/>&gt; dTHX;<br/>&gt; PERL_SET_CONTEXT(my_perl);<br/><br/>I hate the &quot;my_perl&quot; parameter. Who defined it? what is it set to? <br/>Especially if you have more then one perl interpreter in you code. I used:<br/> dTHXa(ph-&gt;perl);<br/> PERL_SET_CONTEXT(ph-&gt;perl);<br/>where ph was some struct that held the perl interpreter for me...<br/><br/>Good luck, anyway.<br/><br/>Shmuel.<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1318.html Tue, 05 Aug 2008 13:04:20 +0000 Re: XS callbacks from different threads. by Josef Wolf Thanks for your efforts, Shmuel!<br/><br/>On Mon, Aug 04, 2008 at 08:10:31PM +0300, Shmuel Fomberg wrote:<br/>&gt; &gt;&gt;IANAE, (I am not an expert) but if you say that the main thread returns <br/>&gt; &gt;&gt;immediately and waiting in a loop for the lib, then you already have one <br/>&gt; &gt;&gt;thread inside Perl. So the background threads shouldn&#39;t call into your <br/>&gt; &gt;&gt;perl interpreter.<br/>&gt; &gt;<br/>&gt; &gt;If I understand correctly, I have two options then:<br/>&gt; &gt;<br/>&gt; &gt;1. _not_ let the main thread return back from the XS. That is, I<br/>&gt; &gt; move the sleep(0) from the perl script into the init() function.<br/>&gt; &gt; Would that break anything withhin perl?<br/>&gt; <br/>&gt; For this one you need a real expert. It is possible to call into Perl <br/>&gt; from an XS sub. but to call into Perl from other threads while it is <br/>&gt; inside a XS call? I have no idea.<br/><br/>As far as I understand the corresponding section in perlguts, it should<br/>be possible _if_ PERL_SET_CONTEXT() is called before. But for some<br/>reason it keeps crashing :-(<br/><br/>&gt; &gt;2. I need to create a second perl interpreter (or clone the first one).<br/>&gt; &gt; Can somebody point me to example code how to do that?<br/>&gt; <br/>&gt; That&#39;s actually no big deal. look at perldoc page perlembed, there are <br/>&gt; examples.<br/><br/>What I am missing from those examples is the explanation how the<br/>different interpreters would communicate.<br/><br/>And there&#39;s one more complication: the low-level library don&#39;t even<br/>state how many threads will call my callback. Therefore I would<br/>prefer to have only _one_ interpreter and use a mutex to keep the<br/>threads from entering the interpreter simultaneously.<br/><br/>This is how my code (still crashing) looks right now:<br/><br/><br/><br/># define PERL_NO_GET_CONTEXT<br/># include &quot;defs.h&quot;<br/><br/>static SV *callback_ref = (SV*)NULL;<br/><br/>static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;<br/><br/>/* This function works OK when called from module_config() in main thread,<br/> but it crashes on PUSHMARK(SP) when called from other threads<br/>*/<br/>int call_perl (int cnt, ...)<br/>{<br/> dTHX;<br/> dSP;<br/><br/> int i;<br/> I32 numargs;<br/> va_list args;<br/><br/> pthread_mutex_lock(&amp;mutex);<br/><br/> PERL_SET_CONTEXT(my_perl);<br/><br/> ENTER;<br/> SAVETMPS;<br/><br/> PUSHMARK(SP); /* Here it crashes when called from different thread */<br/><br/> va_start (args, cnt);<br/> while (cnt-- &gt; 0) {<br/> char *str = va_arg(args, char*);<br/> XPUSHs(sv_2mortal (newSVpv (str, strlen (str))));<br/> }<br/> va_end (args);<br/><br/> PUTBACK;<br/><br/> numargs = call_sv (callback_ref, G_SCALAR);<br/><br/> if (numargs != 1)<br/> croak(&quot;Big trouble\n&quot;);<br/><br/> SPAGAIN;<br/><br/> for (i=0; i&lt;numargs; i++) {<br/> printf (&quot;ret %i\n&quot;, POPi);<br/> }<br/><br/> PUTBACK;<br/> FREETMPS;<br/> LEAVE;<br/><br/> pthread_mutex_unlock(&amp;mutex);<br/><br/> return numargs;<br/>}<br/><br/>void module_config (SV *callback)<br/>{<br/> dTHX;<br/><br/> if (callback_ref == (SV*)NULL) {<br/> callback_ref = newSVsv (callback); /* first time, create new SV */<br/> } else {<br/> SvSetSV (callback_ref, callback); /* been here, overwrite */<br/> }<br/><br/> start_threads ();<br/><br/> do {<br/> sleep (180);<br/> } while (call_perl (1, &quot;check&quot;)); /* works OK when called from here */<br/>}<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1317.html Mon, 04 Aug 2008 15:10:26 +0000 Re: XS callbacks from different threads. by Shmuel Fomberg Hi Josef.<br/><br/>&gt;&gt; IANAE, (I am not an expert) but if you say that the main thread returns <br/>&gt;&gt; immediately and waiting in a loop for the lib, then you already have one <br/>&gt;&gt; thread inside Perl. So the background threads shouldn&#39;t call into your <br/>&gt;&gt; perl interpreter.<br/>&gt; <br/>&gt; If I understand correctly, I have two options then:<br/>&gt; <br/>&gt; 1. _not_ let the main thread return back from the XS. That is, I<br/>&gt; move the sleep(0) from the perl script into the init() function.<br/>&gt; Would that break anything withhin perl?<br/><br/>For this one you need a real expert. It is possible to call into Perl <br/>from an XS sub. but to call into Perl from other threads while it is <br/>inside a XS call? I have no idea.<br/><br/>&gt; 2. I need to create a second perl interpreter (or clone the first one).<br/>&gt; Can somebody point me to example code how to do that?<br/><br/>That&#39;s actually no big deal. look at perldoc page perlembed, there are <br/>examples.<br/><br/>Shmuel.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1316.html Mon, 04 Aug 2008 10:12:06 +0000 Re: XS callbacks from different threads. by Josef Wolf On Sun, Aug 03, 2008 at 10:07:26PM +0300, Shmuel Fomberg wrote:<br/>&gt; Josef Wolf wrote:<br/>&gt; <br/>&gt; &gt; IIUC, I don&#39;t need to create multiple PerlInterpreter&#39;s. I just need<br/>&gt; &gt; to set up the TLS properly in each thread. But it is not clear to me<br/>&gt; &gt; how to do that.<br/>&gt; &gt; # Main thread returns immediately after creating background threads<br/>&gt; &gt; # For now we only sit and wait until we get killed. In a future<br/>&gt; &gt; # improvement we might periodically check for configuration<br/>&gt; &gt; # changes and restart.<br/>&gt; <br/>&gt; IANAE, (I am not an expert) but if you say that the main thread returns <br/>&gt; immediately and waiting in a loop for the lib, then you already have one <br/>&gt; thread inside Perl. So the background threads shouldn&#39;t call into your <br/>&gt; perl interpreter.<br/><br/>Thanks for your answer, Shmuel!<br/><br/>If I understand correctly, I have two options then:<br/><br/>1. _not_ let the main thread return back from the XS. That is, I<br/> move the sleep(0) from the perl script into the init() function.<br/> Would that break anything withhin perl?<br/><br/>2. I need to create a second perl interpreter (or clone the first one).<br/> Can somebody point me to example code how to do that?<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1315.html Sun, 03 Aug 2008 23:10:27 +0000 Re: XS callbacks from different threads. by Shmuel Fomberg Josef Wolf wrote:<br/><br/> &gt; IIUC, I don&#39;t need to create multiple PerlInterpreter&#39;s. I just need<br/> &gt; to set up the TLS properly in each thread. But it is not clear to me<br/> &gt; how to do that.<br/> &gt; # Main thread returns immediately after creating background threads<br/> &gt; # For now we only sit and wait until we get killed. In a future<br/> &gt; # improvement we might periodically check for configuration<br/> &gt; # changes and restart.<br/><br/>IANAE, (I am not an expert) but if you say that the main thread returns <br/>immediately and waiting in a loop for the lib, then you already have one <br/>thread inside Perl. So the background threads shouldn&#39;t call into your <br/>perl interpreter.<br/><br/>Shmuel.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1314.html Sun, 03 Aug 2008 12:07:38 +0000 XS callbacks from different threads. by Josef Wolf Hello,<br/><br/>I am trying to write a XS module for a C library that creates its own<br/>threads from which I have to pass callbacks back into perl.<br/><br/>Unfortunately, I am pretty much confused by the discussion of threads<br/>in perlguts.<br/><br/>IIUC, I don&#39;t need to create multiple PerlInterpreter&#39;s. I just need<br/>to set up the TLS properly in each thread. But it is not clear to me<br/>how to do that.<br/><br/>From the perl point of view, this library would be used like this:<br/><br/> use Mylib;<br/><br/> if (Mylib::init()) { # this will create the additional threads<br/><br/> # Main thread returns immediately after creating background threads<br/> # For now we only sit and wait until we get killed. In a future<br/> # improvement we might periodically check for configuration<br/> # changes and restart.<br/> #<br/> sleep (0);<br/><br/> Mylib::shutdown(); # Cleanup<br/> }<br/><br/> sub callback {<br/> # this will be called when the other threads want to notify<br/> # any events<br/> }<br/><br/><br/>The XS part of the interface seems to be easy:<br/><br/><br/> #include &quot;EXTERN.h&quot;<br/> #include &quot;perl.h&quot;<br/> #include &quot;XSUB.h&quot;<br/> #include &quot;ppport.h&quot;<br/> #include &lt;/m/l/misc/mylib/mylib.h&gt;<br/> <br/> MODULE = Mylib PACKAGE = Mylib<br/> <br/> void<br/> deinit()<br/> <br/> int<br/> init()<br/><br/><br/>And here is the C part of the interface into the library:<br/><br/><br/> /* Protects against concurrent calls from different threads into perl<br/> */<br/> static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;<br/> <br/> /* This function will be called by the threads created in lib_init()<br/> */ <br/> void test_callback (void)<br/> {<br/> dTHX;<br/> dSP;<br/> <br/> pthread_mutex_lock(&amp;mutex);<br/> <br/> PUSHMARK(SP);<br/> XPUSHs(sv_2mortal(newSVpv(&quot;some&quot;, 4)));<br/> XPUSHs(sv_2mortal(newSVpv(&quot;test&quot;, 4)));<br/> XPUSHs(sv_2mortal(newSVpv(&quot;vals&quot;, 4)));<br/> PUTBACK;<br/> <br/> call_pv(&quot;callback&quot;, G_DISCARD);<br/> <br/> pthread_mutex_unlock(&amp;mutex);<br/> }<br/> <br/> int init (void)<br/> {<br/> /* For test purposes we call this from the main thread before any<br/> other threads are created<br/> */<br/> test_callback();<br/> <br/> /* the lib_init() function will create the additional threads<br/> which in turn will be calling test_callback()<br/> */<br/> return lib_init ();<br/> }<br/> <br/> void deinit (void)<br/> {<br/> lib_deinit ();<br/> }<br/><br/><br/>Any hints? Or maybe a pointer to an example?<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/08/msg1313.html Fri, 01 Aug 2008 18:00:31 +0000 Re: Thread::pool by Javier Hi!,<br/><br/>I understand that, to initialize the $pool ideally between daemon()<br/>and scan_files(), just after the fork.<br/>The code:<br/>....<br/>daemon();<br/>$pool = Thread::Pool-&gt;new(<br/> {<br/> optimize =&gt; &#39;memory&#39;,<br/> do =&gt; \&amp;check_system,<br/> pre =&gt; sub {shift; print &quot;starting a new work with\n&quot; },<br/> post =&gt; sub { print &quot;stopping the work\n&quot; },<br/> frequency =&gt; 10,<br/> autoshutdown =&gt; 1,<br/> workers =&gt; 20,<br/> maxjobs =&gt; 100,<br/> minjobs =&gt; 10,<br/> }<br/>);<br/>scan_files();<br/><br/>And the daemon() function implement the fork.<br/><br/>Well, so the program does not work.<br/>The program execute, but does nothing. It was stopped without doing<br/>anything. Just run once while(1) of scan_files().<br/>I think that is how should the $pool did not exist.<br/><br/>2008/7/29 Jerry D. Hedden &lt;jdhedden@cpan.org&gt;:<br/>&gt;&gt; First i initialice the pool and then i set the process as a daemon. Then i<br/>&gt;&gt; call the scan_files() function.<br/>&gt;<br/>&gt; Nope. That&#39;s no good.<br/>&gt;<br/>&gt;&gt; I also test to initialice the pool inside the daemon() function<br/>&gt;<br/>&gt; Depends on where in daemon you do it - it must come after the fork.<br/>&gt;<br/>&gt;&gt; and between daemon() and scan_files().<br/>&gt;<br/>&gt; Better.<br/>&gt;<br/>&gt;&gt; Doing that, the output is blank, but it doesn&#39;t work.<br/>&gt;<br/>&gt; Define &quot;doesn&#39;t work&quot;.<br/>&gt;<br/><br/><br/><br/>-- <br/>A greeting,<br/><br/>Javier.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1312.html Wed, 30 Jul 2008 01:11:56 +0000 Re: Thread::pool by Javier Hi!<br/><br/>scan_files() is a function. Where is? This function is present in same<br/>file. This code is very simple:<br/>= = = = * * * * = = = =<br/>sub scan_files<br/>{<br/> my $DIR = &quot;/var/workq/&quot;;<br/> while(1)<br/> {<br/> opendir(DIRHANDLE, $DIR) || die &quot;ERROR: $DIR not read\n&quot;;<br/> foreach (readdir(DIRHANDLE))<br/> {<br/> if (!-d $_)<br/> {<br/> $pool-&gt;job($_);<br/> }<br/> }<br/> closedir DIRHANDLE;<br/> sleep($Fax::Config::param{&#39;queuescanperiod&#39;});<br/> }<br/>}<br/>= = = = * * * * = = = =<br/><br/>2008/7/29 Chris Fowler &lt;cfowler@outpostsentinel.com&gt;:<br/>&gt; Where is scan_files()?<br/>&gt;<br/>&gt; --<br/>&gt; Chris Fowler<br/>&gt; OutPost Sentinel, LLC<br/>&gt; AIM: strtok2002<br/>&gt; Support @ SIP/support@pbx.opsdc.com<br/>&gt; or 678-804-8193<br/>&gt; Email Support @ support@outpostsentinel.com<br/>&gt;<br/>&gt;<br/>&gt;<br/><br/><br/><br/>-- <br/>A greeting,<br/><br/>Javier.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1311.html Wed, 30 Jul 2008 01:00:35 +0000 Re: Thread::pool by Jerry D. Hedden &gt; First i initialice the pool and then i set the process as a daemon. Then i<br/>&gt; call the scan_files() function.<br/><br/>Nope. That&#39;s no good.<br/><br/>&gt; I also test to initialice the pool inside the daemon() function<br/><br/>Depends on where in daemon you do it - it must come after the fork.<br/><br/>&gt; and between daemon() and scan_files().<br/><br/>Better.<br/><br/>&gt; Doing that, the output is blank, but it doesn&#39;t work.<br/><br/>Define &quot;doesn&#39;t work&quot;.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1310.html Tue, 29 Jul 2008 05:39:28 +0000 Re: Thread::pool by Chris Fowler Where is scan_files()?<br/><br/>-- <br/>Chris Fowler<br/>OutPost Sentinel, LLC<br/>AIM: strtok2002<br/>Support @ SIP/support@pbx.opsdc.com<br/> or 678-804-8193<br/>Email Support @ support@outpostsentinel.com<br/><br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1309.html Tue, 29 Jul 2008 05:28:25 +0000 Thread::pool by Javier Hi,<br/><br/>I have a problem with the module Thread::Pool-0.32.<br/>I need to set my script as a daemon, but when i use this module the program<br/>kill the threads.<br/><br/>The output of my script is:<br/><br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> starting a new work with<br/> Perl exited with active threads:<br/> 20 running and unjoined<br/> 0 finished and unjoined<br/> 0 running and detached<br/><br/>And the source code is:<br/>= = = = = = = = * * * * * * * * * * = = = = = = = =<br/>my $pool = Thread::Pool-&gt;new(<br/> {<br/> optimize =&gt; &#39;memory&#39;,<br/> do =&gt; \&amp;check_system,<br/> pre =&gt; sub {shift; print &quot;starting a new work with<br/>@_\n&quot; },<br/> post =&gt; sub { print &quot;stopping the work\n&quot; },<br/> frequency =&gt; 10,<br/> autoshutdown =&gt; 1,<br/> workers =&gt; 20,<br/> maxjobs =&gt; 100,<br/> minjobs =&gt; 10,<br/> }<br/> );<br/><br/>sub daemon<br/>{<br/> chdir &quot;/&quot;;<br/> open STDIN, &#39;/dev/null&#39;;<br/> fork &amp;&amp; exit;<br/> setsid();<br/> if (open PID, &#39;&gt;&#39;, &#39;/var/run/faxqr.pid&#39;)<br/> {<br/> print PID &quot;$$\n&quot;;<br/> close PID;<br/> }<br/>}<br/><br/>daemon();<br/>scan_files();<br/>= = = = = = = = * * * * * * * * * * = = = = = = = =<br/><br/>First i initialice the pool and then i set the process as a daemon. Then i<br/>call the scan_files() function.<br/><br/>I also test to initialice the pool inside the daemon() function and between<br/>daemon() and scan_files(). Doing that, the output is blank, but it doesn&#39;t<br/>work.<br/><br/>Thank you for your help.<br/><br/>-- <br/>A greeting,<br/><br/>Javier.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1308.html Tue, 29 Jul 2008 05:24:08 +0000 Re: Calling a object method in a create() by Valter Douglas Lisbôa Jr. Thanks, it worked! <br/><br/>On Monday 14 July 2008 12:41:28 Jerry D. Hedden wrote:<br/>&gt; Valter Douglas Lisb&ocirc;a Jr. wrote:<br/>&gt; &gt; The code bellow functions very nice.<br/>&gt; &gt;<br/>&gt; &gt; ---------------------<br/>&gt; &gt; while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) =<br/>&gt; &gt; $sthLocals-&gt;fetchrow_array()) {<br/>&gt; &gt; $self-&gt;localPolling($id, $ep, $local, $tolerance, $lasterr,<br/>&gt; &gt; $hostscount); }<br/>&gt; &gt; ---------------------<br/>&gt; &gt;<br/>&gt; &gt; But if I try to execute with a thread:<br/>&gt; &gt;<br/>&gt; &gt; ---------------------<br/>&gt; &gt; while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) =<br/>&gt; &gt; $sthLocals-&gt;fetchrow_array()) {<br/>&gt; &gt; my $thr = threads-&gt;create($self-&gt;localPooling, $id, $ep, $local,<br/>&gt; &gt; $tolerance, $lasterr, $hostscount);<br/>&gt; &gt; }<br/>&gt; &gt; ---------------------<br/>&gt; &gt;<br/>&gt; &gt; It returns a error:<br/>&gt; &gt; Can&#39;t locate object method &quot;localPooling&quot; via package &quot;Poller::Check&quot;!<br/>&gt;<br/>&gt; Try<br/>&gt;<br/>&gt; while (my ($id, $ep, $local, $tolerance, $lasterr, $hostscount)<br/>&gt; = $sthLocals-&gt;fetchrow_array())<br/>&gt; {<br/>&gt; my $thr = threads-&gt;create(sub {<br/>&gt; $self-&gt;localPooling($id, $ep, $local, $tolerance, $lasterr,<br/>&gt; $hostscount);<br/>&gt; });<br/>&gt; }<br/><br/><br/><br/>-- <br/>Valter Douglas Lisb&ocirc;a Jr.<br/>S&oacute;cio-Diretor<br/>Trenix - IT Solutions<br/>&quot;Nossas Id&eacute;ias, suas Solu&ccedil;&otilde;es!&quot;<br/>www.trenix.com.br<br/>contato@trenix.com.br<br/>Tel. +55 19 3402.2957<br/>Cel. +55 19 9183.4244<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1307.html Mon, 14 Jul 2008 09:33:37 +0000 Re: Calling a object method in a create() by Jerry D. Hedden Valter Douglas Lisb&ocirc;a Jr. wrote:<br/>&gt; The code bellow functions very nice.<br/>&gt;<br/>&gt; ---------------------<br/>&gt; while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) =<br/>&gt; $sthLocals-&gt;fetchrow_array()) {<br/>&gt; $self-&gt;localPolling($id, $ep, $local, $tolerance, $lasterr, $hostscount);<br/>&gt; }<br/>&gt; ---------------------<br/>&gt;<br/>&gt; But if I try to execute with a thread:<br/>&gt;<br/>&gt; ---------------------<br/>&gt; while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) =<br/>&gt; $sthLocals-&gt;fetchrow_array()) {<br/>&gt; my $thr = threads-&gt;create($self-&gt;localPooling, $id, $ep, $local, $tolerance,<br/>&gt; $lasterr, $hostscount);<br/>&gt; }<br/>&gt; ---------------------<br/>&gt;<br/>&gt; It returns a error:<br/>&gt; Can&#39;t locate object method &quot;localPooling&quot; via package &quot;Poller::Check&quot;!<br/><br/>Try<br/><br/>while (my ($id, $ep, $local, $tolerance, $lasterr, $hostscount)<br/> = $sthLocals-&gt;fetchrow_array())<br/>{<br/> my $thr = threads-&gt;create(sub {<br/> $self-&gt;localPooling($id, $ep, $local, $tolerance, $lasterr,<br/>$hostscount);<br/> });<br/>}<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1306.html Mon, 14 Jul 2008 08:41:37 +0000 Calling a object method in a create() by Valter Douglas Lisbôa Jr. Hi all,<br/><br/>I new on threads, but not so on perl.<br/><br/>I trying to execute a thread inside a object method of a class that I make to <br/>consult SNMP quests and feed a rrddatabase.<br/><br/>The code bellow functions very nice.<br/><br/>---------------------<br/>while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) = <br/>$sthLocals-&gt;fetchrow_array()) {<br/> $self-&gt;localPolling($id, $ep, $local, $tolerance, $lasterr, $hostscount);<br/>}<br/>---------------------<br/><br/>But if I try to execute with a thread:<br/><br/>---------------------<br/>while(my ($id, $ep, $local, $tolerance, $lasterr, $hostscount) = <br/>$sthLocals-&gt;fetchrow_array()) {<br/> my $thr = threads-&gt;create($self-&gt;localPooling, $id, $ep, $local, $tolerance, <br/>$lasterr, $hostscount);<br/>}<br/>---------------------<br/><br/>It returns a error:<br/>Can&#39;t locate object method &quot;localPooling&quot; via package &quot;Poller::Check&quot;!<br/><br/>I try to put a \&amp; before $self-&gt; and take another error:<br/>Not a code reference<br/><br/>Are there a way to call a class $self-&gt;method in a thread? I search across <br/>Google and not find any clue.<br/><br/>Thanks in advance.<br/><br/>-- <br/>Valter Douglas Lisb&Atilde;&acute;a Jr.<br/>S&Atilde;&sup3;cio-Diretor<br/>Trenix - IT Solutions<br/>&quot;Nossas Id&Atilde;&copy;ias, suas Solu&Atilde;&sect;&Atilde;&micro;es!&quot;<br/>www.trenix.com.br<br/>contato@trenix.com.br<br/>Tel. +55 19 3402.2957<br/>Cel. +55 19 9183.4244<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/07/msg1305.html Mon, 14 Jul 2008 07:22:53 +0000 environment variables by Wong, Danny H. Hi All,<br/> I have a script that set environment variables for 10 plus<br/>configuration and each configuration share some environment variable and<br/>have some different or specific environment variables for a particular<br/>configuration. I set the env variables before calling each thread, but I<br/>notice that the environment variable inherits the previous<br/>configurations environment setting.<br/>Example:<br/>configure 1:<br/>set 11 variables<br/><br/>Configure 2:<br/>set 10 variables, all of of configure minus 1 specific variable<br/><br/>But configure 2 has all eleven variables.<br/><br/>Is there a way to delete the environment variables before setting each<br/>configuration?<br/><br/>I tried<br/>undef (%ENV)<br/>SETENV ();<br/><br/>but no luck?<br/><br/>Also, I read that environment variables in Linux are cached? If it true,<br/>is there a way to clear the cache without rebooting the system?<br/><br/>Any ideas how I can delete all the environment variables before setting<br/>them again for the next configuration?<br/><br/>thanks in advance.<br/><br/><br/><br/><br/> - - - - - Cisco - - - - -<br/>This e-mail and any attachments may contain information which is confidential,<br/>proprietary, privileged or otherwise protected by law. The information is solely<br/>intended for the named addressee (or a person responsible for delivering it to<br/>the addressee). If you are not the intended recipient of this message, you are<br/>not authorized to read, print, retain, copy or disseminate this message or any<br/>part of it. If you have received this e-mail in error, please notify the sender<br/>immediately by return e-mail and delete it from your computer.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1304.html Fri, 13 Jun 2008 17:15:54 +0000 Re: Memory usage by Javier Terceiro 2008/6/11 Jerry D. Hedden &lt;jdhedden@cpan.org&gt;:<br/>&gt; On Wed, Jun 11, 2008 at 9:26 AM, Javier Terceiro &lt;correolista@gmail.com&gt; wrote:<br/>&gt;&gt; Hello list,<br/>&gt;&gt;<br/>&gt;&gt; I am designed a new software. I am using threads. The operation is<br/>&gt;&gt; correct, but memory consumption is excessive. The system is using 100<br/>&gt;&gt; MB and when the software is working, 500 Mb. The memory is not<br/>&gt;&gt; released, why?<br/>&gt;<br/>&gt; You need to either join the threads after they&#39;re done working, or<br/>&gt; else detach them. See below.<br/>&gt;<br/>&gt;&gt;<br/>&gt;&gt; The source code is:<br/>&gt;&gt;<br/>&gt;&gt; -----------------------------------------------------------------------------<br/>&gt;&gt;<br/>&gt;&gt; #!/usr/bin/perl -w<br/>&gt;&gt;<br/>&gt;&gt; use strict;<br/>&gt;&gt; use warnings;<br/>&gt;&gt; use AnyData;<br/>&gt;&gt; use Sys::Syslog;<br/>&gt;&gt; use Sys::Syslog qw(:DEFAULT setlogsock);<br/>&gt;&gt; use Sys::Syslog qw(:standard :macros);<br/>&gt;&gt; use POSIX qw(setsid);<br/>&gt;&gt; use threads;<br/>&gt;&gt; use threads::shared;<br/>&gt;&gt;<br/>&gt;&gt; sub mail<br/>&gt;&gt; {<br/>&gt;&gt; print (&quot;The mail thread\n&quot;);<br/>&gt;&gt; return 0;<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt; sub mf<br/>&gt;&gt; {<br/>&gt;&gt; print (&quot;The mf thread\n&quot;);<br/>&gt;&gt; return 0;<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt; sub scan_files<br/>&gt;&gt; {<br/>&gt;&gt; while(1)<br/>&gt;&gt; {<br/>&gt;&gt; foreach (readdir(DIRHANDLE))<br/>&gt;&gt; {<br/>&gt;&gt; if (!-d $_)<br/>&gt;&gt; {<br/>&gt;&gt; my $opt =<br/>&gt;&gt; choose_file_option(&quot;workq/&quot;,$_,&quot;Delivered&quot;);<br/>&gt;&gt; switch ($opt)<br/>&gt;&gt; {<br/>&gt;&gt; case &quot;mail&quot;<br/>&gt;&gt; {<br/>&gt;&gt; threads-&gt;new(\&amp;mail, $_);<br/>&gt;<br/>&gt; threads-&gt;new(\&amp;main, $_)-&gt;detach();<br/>&gt;<br/>&gt;&gt; }<br/>&gt;&gt; case &quot;mf&quot;<br/>&gt;&gt; {<br/>&gt;&gt; threads-&gt;new(\&amp;mf, $_);<br/>&gt;<br/>&gt; threads-&gt;new(\&amp;mf, $_)-&gt;detach();<br/>&gt;<br/>&gt;&gt; }<br/>&gt;&gt; else<br/>&gt;&gt; {<br/>&gt;&gt; syslog (&#39;daemon|info&#39;,<br/>&gt;&gt; &quot;Send mail. Failure detected&quot;);<br/>&gt;&gt; }<br/>&gt;&gt; }<br/>&gt;&gt; }<br/>&gt;&gt; }<br/>&gt;&gt; sleep(60);<br/>&gt;&gt; }<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt; sub daemon<br/>&gt;&gt; {<br/>&gt;&gt; chdir &quot;/&quot;;<br/>&gt;&gt; open STDIN, &#39;/dev/null&#39;;<br/>&gt;&gt; open STDOUT, &#39;&gt;/dev/null&#39;;<br/>&gt;&gt; open STDERR, &#39;&gt;/dev/null&#39;;<br/>&gt;&gt; fork &amp;&amp; exit;<br/>&gt;&gt; setsid();<br/>&gt;&gt; if (open PID, &#39;&gt;&#39;, &#39;/var/run/program.pid&#39;)<br/>&gt;&gt; {<br/>&gt;&gt; print PID &quot;$$\n&quot;;<br/>&gt;&gt; close PID;<br/>&gt;&gt; }<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt; setlogmask( LOG_MASK($Fax::Config::param{&#39;log_info&#39;}) );<br/>&gt;&gt; openlog &#39;faxqr&#39;, &#39;pid&#39;, LOG_DAEMON;<br/>&gt;&gt; daemon();<br/>&gt;&gt;<br/>&gt;&gt; $SIG{&quot;TERM&quot;} = \&amp;notify_exit;<br/>&gt;&gt;<br/>&gt;&gt; scan_files();<br/>&gt;&gt;<br/>&gt;&gt; -----------------------------------------------------------------------------<br/>&gt;&gt;<br/>&gt;&gt; Any idea that there is a very high consumption of memory.<br/>&gt;&gt;<br/>&gt;&gt; --<br/>&gt;&gt; A greeting,<br/>&gt;&gt;<br/>&gt;&gt; Javier.<br/>&gt;&gt;<br/>&gt;<br/><br/>Hi,<br/><br/>I aply this changes, but now I obtein a fatal error:<br/><br/>*** glibc detected *** double free or corruption (!prev): 0x08fea490 ***<br/><br/>And the process is cancel :(<br/><br/>Any idea?<br/><br/>-- <br/>A greeting,<br/><br/>Javier.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1303.html Wed, 11 Jun 2008 07:59:01 +0000 Re: Memory usage by Jerry D. Hedden On Wed, Jun 11, 2008 at 9:26 AM, Javier Terceiro &lt;correolista@gmail.com&gt; wrote:<br/>&gt; Hello list,<br/>&gt;<br/>&gt; I am designed a new software. I am using threads. The operation is<br/>&gt; correct, but memory consumption is excessive. The system is using 100<br/>&gt; MB and when the software is working, 500 Mb. The memory is not<br/>&gt; released, why?<br/><br/>You need to either join the threads after they&#39;re done working, or<br/>else detach them. See below.<br/><br/>&gt;<br/>&gt; The source code is:<br/>&gt;<br/>&gt; -----------------------------------------------------------------------------<br/>&gt;<br/>&gt; #!/usr/bin/perl -w<br/>&gt;<br/>&gt; use strict;<br/>&gt; use warnings;<br/>&gt; use AnyData;<br/>&gt; use Sys::Syslog;<br/>&gt; use Sys::Syslog qw(:DEFAULT setlogsock);<br/>&gt; use Sys::Syslog qw(:standard :macros);<br/>&gt; use POSIX qw(setsid);<br/>&gt; use threads;<br/>&gt; use threads::shared;<br/>&gt;<br/>&gt; sub mail<br/>&gt; {<br/>&gt; print (&quot;The mail thread\n&quot;);<br/>&gt; return 0;<br/>&gt; }<br/>&gt;<br/>&gt; sub mf<br/>&gt; {<br/>&gt; print (&quot;The mf thread\n&quot;);<br/>&gt; return 0;<br/>&gt; }<br/>&gt;<br/>&gt; sub scan_files<br/>&gt; {<br/>&gt; while(1)<br/>&gt; {<br/>&gt; foreach (readdir(DIRHANDLE))<br/>&gt; {<br/>&gt; if (!-d $_)<br/>&gt; {<br/>&gt; my $opt =<br/>&gt; choose_file_option(&quot;workq/&quot;,$_,&quot;Delivered&quot;);<br/>&gt; switch ($opt)<br/>&gt; {<br/>&gt; case &quot;mail&quot;<br/>&gt; {<br/>&gt; threads-&gt;new(\&amp;mail, $_);<br/><br/>threads-&gt;new(\&amp;main, $_)-&gt;detach();<br/><br/>&gt; }<br/>&gt; case &quot;mf&quot;<br/>&gt; {<br/>&gt; threads-&gt;new(\&amp;mf, $_);<br/><br/>threads-&gt;new(\&amp;mf, $_)-&gt;detach();<br/><br/>&gt; }<br/>&gt; else<br/>&gt; {<br/>&gt; syslog (&#39;daemon|info&#39;,<br/>&gt; &quot;Send mail. Failure detected&quot;);<br/>&gt; }<br/>&gt; }<br/>&gt; }<br/>&gt; }<br/>&gt; sleep(60);<br/>&gt; }<br/>&gt; }<br/>&gt;<br/>&gt; sub daemon<br/>&gt; {<br/>&gt; chdir &quot;/&quot;;<br/>&gt; open STDIN, &#39;/dev/null&#39;;<br/>&gt; open STDOUT, &#39;&gt;/dev/null&#39;;<br/>&gt; open STDERR, &#39;&gt;/dev/null&#39;;<br/>&gt; fork &amp;&amp; exit;<br/>&gt; setsid();<br/>&gt; if (open PID, &#39;&gt;&#39;, &#39;/var/run/program.pid&#39;)<br/>&gt; {<br/>&gt; print PID &quot;$$\n&quot;;<br/>&gt; close PID;<br/>&gt; }<br/>&gt; }<br/>&gt;<br/>&gt; setlogmask( LOG_MASK($Fax::Config::param{&#39;log_info&#39;}) );<br/>&gt; openlog &#39;faxqr&#39;, &#39;pid&#39;, LOG_DAEMON;<br/>&gt; daemon();<br/>&gt;<br/>&gt; $SIG{&quot;TERM&quot;} = \&amp;notify_exit;<br/>&gt;<br/>&gt; scan_files();<br/>&gt;<br/>&gt; -----------------------------------------------------------------------------<br/>&gt;<br/>&gt; Any idea that there is a very high consumption of memory.<br/>&gt;<br/>&gt; --<br/>&gt; A greeting,<br/>&gt;<br/>&gt; Javier.<br/>&gt;<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1302.html Wed, 11 Jun 2008 06:32:32 +0000 Memory usage by Javier Terceiro Hello list,<br/><br/>I am designed a new software. I am using threads. The operation is<br/>correct, but memory consumption is excessive. The system is using 100<br/>MB and when the software is working, 500 Mb. The memory is not<br/>released, why?<br/><br/>The source code is:<br/><br/>-----------------------------------------------------------------------------<br/><br/>#!/usr/bin/perl -w<br/><br/>use strict;<br/>use warnings;<br/>use AnyData;<br/>use Sys::Syslog;<br/>use Sys::Syslog qw(:DEFAULT setlogsock);<br/>use Sys::Syslog qw(:standard :macros);<br/>use POSIX qw(setsid);<br/>use threads;<br/>use threads::shared;<br/><br/>sub mail<br/>{<br/> print (&quot;The mail thread\n&quot;);<br/> return 0;<br/>}<br/><br/>sub mf<br/>{<br/> print (&quot;The mf thread\n&quot;);<br/> return 0;<br/>}<br/><br/>sub scan_files<br/>{<br/> while(1)<br/> {<br/> foreach (readdir(DIRHANDLE))<br/> {<br/> if (!-d $_)<br/> {<br/> my $opt =<br/>choose_file_option(&quot;workq/&quot;,$_,&quot;Delivered&quot;);<br/> switch ($opt)<br/> {<br/> case &quot;mail&quot;<br/> {<br/> threads-&gt;new(\&amp;mail, $_);<br/> }<br/> case &quot;mf&quot;<br/> {<br/> threads-&gt;new(\&amp;mf, $_);<br/> }<br/> else<br/> {<br/> syslog (&#39;daemon|info&#39;,<br/>&quot;Send mail. Failure detected&quot;);<br/> }<br/> }<br/> }<br/> }<br/> sleep(60);<br/> }<br/>}<br/><br/>sub daemon<br/>{<br/> chdir &quot;/&quot;;<br/> open STDIN, &#39;/dev/null&#39;;<br/> open STDOUT, &#39;&gt;/dev/null&#39;;<br/> open STDERR, &#39;&gt;/dev/null&#39;;<br/> fork &amp;&amp; exit;<br/> setsid();<br/> if (open PID, &#39;&gt;&#39;, &#39;/var/run/program.pid&#39;)<br/> {<br/> print PID &quot;$$\n&quot;;<br/> close PID;<br/> }<br/>}<br/><br/>setlogmask( LOG_MASK($Fax::Config::param{&#39;log_info&#39;}) );<br/>openlog &#39;faxqr&#39;, &#39;pid&#39;, LOG_DAEMON;<br/>daemon();<br/><br/>$SIG{&quot;TERM&quot;} = \&amp;notify_exit;<br/><br/>scan_files();<br/><br/>-----------------------------------------------------------------------------<br/><br/>Any idea that there is a very high consumption of memory.<br/><br/>-- <br/>A greeting,<br/><br/>Javier.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1301.html Wed, 11 Jun 2008 06:27:07 +0000 Re: thread hash bug? by Jerry D. Hedden Wong, Danny H. wrote:<br/>&gt; I&#39;m trying to created a hash of array inside a thread, but the hash<br/>&gt; loses all its keys/values when leaving the function. Is there a bug in<br/>&gt; using a hash inside a thread? Any ideas?<br/><br/>1. use strict;<br/>2. use warnings;<br/>3. Read &#39;perldoc threads::shared&#39;<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1300.html Wed, 04 Jun 2008 06:15:17 +0000 thread hash bug? by Wong, Danny H. Hi GURUS,<br/><br/>I&#39;m trying to created a hash of array inside a thread, but the hash<br/>loses all its keys/values when leaving the function. Is there a bug in<br/>using a hash inside a thread? Any ideas?<br/><br/>foreach my $i ( 1..3){<br/><br/>push (@BUILD_THREADS, threads-&gt;create(\&amp;COPY, $i));<br/><br/>}<br/><br/>foreach my $THREAD (@BUILD_THREADS)<br/><br/>{<br/><br/>my @DATA = $THREAD-&gt;join();<br/><br/>}<br/><br/>sub COPY<br/><br/>{<br/><br/>my ($i) = @_;<br/><br/>copy ( ${SOURCE}, &quot;${DESTINATION}_${i}&quot;);<br/><br/>push @{$STORE_BUILD_RESULTS{&quot;flintstones&quot;}}, &quot;${DESTINATION}_${i}&quot;;<br/><br/>while ( my ($KEY, $VALUE) = each (%STORE_BUILD_RESULTS))<br/><br/>{<br/><br/>print &quot;KEY, VALUE is $KEY, @$VALUE\n&quot;; #I get correct print out.<br/><br/># $COMPLETED_BUILDS = STORE_BUILD_RESULTS<br/><br/>}<br/><br/>}<br/><br/>while ( my ($KEY, $VALUE) = each (%STORE_BUILD_RESULTS)) #The hash is<br/>empty. Nothing gets printed out.<br/><br/>{<br/><br/>print &quot;KEY, VALUE is $KEY, $VALUE\n&quot;;<br/><br/># $COMPLETED_BUILDS = STORE_BUILD_RESULTS<br/><br/>}<br/><br/><br/><br/><br/><br/> - - - - - Cisco - - - - -<br/>This e-mail and any attachments may contain information which is confidential,<br/>proprietary, privileged or otherwise protected by law. The information is solely<br/>intended for the named addressee (or a person responsible for delivering it to<br/>the addressee). If you are not the intended recipient of this message, you are<br/>not authorized to read, print, retain, copy or disseminate this message or any<br/>part of it. If you have received this e-mail in error, please notify the sender<br/>immediately by return e-mail and delete it from your computer.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/06/msg1299.html Wed, 04 Jun 2008 02:08:54 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden Jerry D. Hedden wrote:<br/>&gt;<br/>&gt; perhaps we could just provide a function:<br/>&gt;<br/>&gt; $y = shared_clone($x);<br/><br/>I uploaded threads::shared 1.21 which now has this functionality.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1298.html Sat, 17 May 2008 04:59:27 +0000 Problem in build perl5.8.7 on OpenVMS using threads by ravi kumar Hi,<br/><br/>I am getting the fallowing error while building the perl5.8.7 on OpenVMS I64<br/>v.8.3 system using threads<br/>MMK<br/> 15-MAY-2008 16:40:40<br/> 15-MAY-2008 16:40:40<br/> 15-MAY-2008 16:40:40<br/>CC/DECC/NOANSI_ALIAS<br/>/Include=[]/Standard=Relaxed_ANSI/Prefix=All/Obj=.obj/List/Machine/Show=Expan/float=ieee/ieee=denorm/Define=(PE<br/>RL_CORE,_LARGEFILE=1) MINIPERLMAIN.C<br/>int Perl_vms_decc_feature_get_index(aTHX_ const char *)<br/>^<br/>%CC-E-NOSEMI, Missing &quot;;&quot;.<br/>at line number 846 in file PERL_BUILD_ROOT:[000000]vmsish.h;1<br/>I32 unlnk (char*);<br/>....^<br/>%CC-E-PARNOIDENT, Missing identifier.<br/>at line number 3188 in file PERL_BUILD_ROOT:[000000]perl.h;1<br/> struct group _grent_struct;<br/>........................^<br/>%CC-E-INCOMPMEM, The member &quot;_grent_struct&quot; has an incomplete type.<br/>at line number 623 in file PERL_BUILD_ROOT:[000000]reentr.h;1<br/> PL_perl_destruct_level = 0;<br/>........^<br/>%CC-W-INCOMPDEREF, In this statement, &quot;my_perl&quot; is a pointer to an<br/>incomplete struct or union and should not be used as the left ope<br/>rand of a member dereference.<br/>at line number 93 in file PERL_BUILD_ROOT:[000000]miniperlmain.c;1<br/> PL_exit_flags |= PERL_EXIT_DESTRUCT_END;<br/>....^<br/>%CC-W-INCOMPDEREF, In this statement, &quot;my_perl&quot; is a pointer to an<br/>incomplete struct or union and should not be used as the left ope<br/>rand of a member dereference.<br/>at line number 95 in file PERL_BUILD_ROOT:[000000]miniperlmain.c;1<br/>%MMK-F-ERRUPD, error status %X10B91262 occurred when updating target<br/>MINIPERLMAIN.OBJ<br/> 15-MAY-2008 16:40:42<br/>$<br/><br/>I have changed the configure.com file by changing the usethreads=&quot;T&quot; and<br/>useithreads=&quot;Y&quot;<br/>also i used configure option as fallows<br/>configure.com - &quot;-Dunlink_all_versions&quot; &quot;-Duselargefiles&quot;<br/>&quot;-Dcf_email=SYSTEM&quot; &quot;-Dusethreads&quot; &quot;-de&quot;<br/><br/>I have built the same without enabling threads and it went fine.<br/><br/>so could you help me out?<br/><br/>-- <br/>Ravikumar karnati<br/><br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1297.html Fri, 16 May 2008 04:22:37 +0000 Re: p5p summary: Improving threads::shared ? by Dean Arnold Jerry D. Hedden wrote:<br/>&gt;&gt; perhaps we could just provide a function:<br/>&gt;&gt;<br/>&gt;&gt; $y = shared_clone($x);<br/>&gt; <br/>&gt; What name should this function have?<br/>&gt; <br/>&gt; shared_clone()<br/>&gt; clone()<br/>&gt; shared_copy()<br/>&gt; copy()<br/>&gt; make_shared()<br/>&gt; <br/>&gt; Or something else?<br/>&gt; <br/><br/>I&#39;d vote against clone() or copy(), as they&#39;re too<br/>general. Otherwise, I personally have no opinion,<br/>tho clone has come to mean this sort of deepcopy<br/>(see the various Clone modules), so I suppose<br/>shared_clone() may make sense.<br/><br/>- Dean<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1296.html Thu, 08 May 2008 08:54:08 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden &gt; perhaps we could just provide a function:<br/>&gt;<br/>&gt; $y = shared_clone($x);<br/><br/>What name should this function have?<br/><br/> shared_clone()<br/> clone()<br/> shared_copy()<br/> copy()<br/> make_shared()<br/><br/>Or something else?<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1295.html Thu, 08 May 2008 07:27:09 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden &gt; For any existing apps (eg, Data::Dumper) that want to<br/>&gt; deal with it, they could always fallback to detecting<br/>&gt; something as shared and saving its id (ie, the shared<br/>&gt; interpretter version&#39;s address) to detect cycles. Not<br/>&gt; pretty, but effective.<br/><br/>I looked into fixing Data::Dumper for this. The circular<br/>reference checking is done both in Perl (for the pure-Perl<br/>version) and in XS (the usual version). It&#39;ll take me<br/>awhile to figure out how to do it in XS.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1294.html Thu, 08 May 2008 07:17:28 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden &gt; &gt; It means that something like this would DWIM:<br/>&gt; &gt;<br/>&gt; &gt; my $x : shared;<br/>&gt; &gt; $x = [ { &#39;complex&#39; =&gt; &#39;aggregate&#39; }, [ qw/ currently not sharable / ]<br/>&gt; ];<br/>&gt; &gt;<br/>&gt; &gt;<br/>&gt; &gt; Then we just need a tie-in with the assignment operator. Or<br/>&gt; &gt; perhaps we could just provide a function:<br/>&gt; &gt;<br/>&gt; &gt; $y = shared_clone($x);<br/>&gt; &gt;<br/>&gt; &gt;<br/>&gt;<br/>&gt; Would an assignment op overload work ?<br/>&gt; I.e., if the LHS was already shared(), then the = overload<br/>&gt; would do the deepcopy ? Or would that break the<br/>&gt; XS tie/magic side of the code ?<br/><br/>Oops. Shared variables aren&#39;t objects. So I don&#39;t think we<br/>can use &#39;overload&#39; on &#39;=&#39;. Is that correct?<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1293.html Thu, 08 May 2008 07:13:11 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden On Mon, May 5, 2008 at 1:53 PM, Jerry D. Hedden &lt;jdhedden@cpan.org&gt; wrote:<br/>&gt; &gt; My sample worked OK *after* I removed the Data::Dumper<br/>&gt; &gt; print; apparently, it has some issues w/ circular<br/>&gt; &gt; references as well (I tried setting Deepcopy and<br/>&gt; &gt; Purity, to no avail); I suspect its due to the fact<br/>&gt; &gt; that references to shared elements don&#39;t have the same<br/>&gt; &gt; stringified value, so it doesn&#39;t actually look like<br/>&gt; &gt; a circular ref.<br/>&gt;<br/>&gt; Oh, yes. That&#39;s part of what I reported here:<br/>&gt;<br/>&gt; http://rt.perl.org/rt3/Public/Bug/Display.html?id=37946<br/>&gt;<br/>&gt; The following highlights the problem:<br/>&gt;<br/>&gt;<br/>&gt; use strict;<br/>&gt; use warnings;<br/>&gt;<br/>&gt; use threads;<br/>&gt; use threads::shared;<br/>&gt;<br/>&gt; my $x;<br/>&gt; $x = \$x;<br/>&gt; share($x);<br/>&gt;<br/>&gt; print(&quot;Look at \$x:\n&quot;);<br/>&gt; print($x, &quot;\n&quot;);<br/>&gt; print($$x, &quot;\n&quot;);<br/>&gt; print($$$x, &quot;\n&quot;);<br/>&gt; print($$$$x, &quot;\n&quot;);<br/>&gt; print($$$$$x, &quot;\n&quot;);<br/>&gt; print($$$$$$x, &quot;\n&quot;);<br/>&gt;<br/>&gt; my @q :shared = ($x);<br/>&gt;<br/>&gt; my $y = $q[0];<br/>&gt;<br/>&gt; print(&quot;\nFirst look at \$y:\n&quot;);<br/>&gt; print($y, &quot;\n&quot;);<br/>&gt; print($$y, &quot;\n&quot;);<br/>&gt; print($$$y, &quot;\n&quot;);<br/>&gt; print($$$$y, &quot;\n&quot;);<br/>&gt; print($$$$$y, &quot;\n&quot;);<br/>&gt; print($$$$$$y, &quot;\n&quot;);<br/>&gt;<br/>&gt; print(&quot;\nSecond look at \$y:\n&quot;);<br/>&gt; print($y, &quot;\n&quot;);<br/>&gt; print($$y, &quot;\n&quot;);<br/>&gt; print($$$y, &quot;\n&quot;);<br/>&gt; print($$$$y, &quot;\n&quot;);<br/>&gt; print($$$$$y, &quot;\n&quot;);<br/>&gt; print($$$$$$y, &quot;\n&quot;);<br/>&gt;<br/>&gt; This outputs:<br/>&gt;<br/>&gt; Look at $x:<br/>&gt; REF(0x144f8f0)<br/>&gt; REF(0x144f8f0)<br/>&gt; REF(0x144f8f0)<br/>&gt; REF(0x144f8f0)<br/>&gt; REF(0x144f8f0)<br/>&gt; REF(0x144f8f0)<br/>&gt;<br/>&gt; First look at $y:<br/>&gt; SCALAR(0x1423c70)<br/>&gt; SCALAR(0x1423ad8)<br/>&gt; SCALAR(0x14bd968)<br/>&gt; SCALAR(0x14bd980)<br/>&gt; SCALAR(0x14bd998)<br/>&gt; SCALAR(0x14bd9b0)<br/>&gt;<br/>&gt; Second look at $y:<br/>&gt; REF(0x1423c70)<br/>&gt; REF(0x1423ad8)<br/>&gt; REF(0x14bd968)<br/>&gt; REF(0x14bd980)<br/>&gt; REF(0x14bd998)<br/>&gt; SCALAR(0x14bd9b0)<br/>&gt;<br/>&gt; Seems to me that this is a bug. It shows that<br/>&gt; threads::shared isn&#39;t detecting that it&#39;s dealing with a<br/>&gt; circular reference. With each level that the app traverses,<br/>&gt; threads::shared &quot;unrolls&quot; another version of the shared<br/>&gt; variable.<br/><br/>I just posted a patch to blead that fixes this. If the patch passes<br/>muster, I&#39;ll release an update for threads::shared to CPAN.<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1292.html Wed, 07 May 2008 11:57:58 +0000 Re: p5p summary: Improving threads::shared ? by Dean Arnold Dean Arnold wrote:<br/>&gt; Jerry D. Hedden wrote:<br/>&gt;&gt; Conceptually, I think it would require keeping a weak<br/>&gt;&gt; reference of each private SV associated with a shared SV.<br/>&gt;&gt; This would need to be done on a per-thread basis. Then when<br/>&gt;&gt; a thread tries to reference a shared SV, a lookup would be<br/>&gt;&gt; made to see if a private SV already exists (and still<br/>&gt;&gt; exists) for that thread. If so, that is returned (and the<br/>&gt;&gt; ref count of the weak ref is incremented?). If not, a new<br/>&gt;&gt; private SV is created, and a weak ref to it is appropriately<br/>&gt;&gt; stored.<br/>&gt;&gt;<br/>&gt;&gt; Is this a logical approach? If so, is it doable?<br/>&gt;&gt;<br/>&gt; <br/>&gt; For threads::shared, the only solution I can think of is<br/>&gt; adding a fieldhash to the thread-private my_ctx structure<br/>&gt; keyed by the address of the referent variable&#39;s<br/>&gt; shared version. Then each attempt to create a new proxy<br/>&gt; would lookup any existing persistent proxy for the shared<br/>&gt; SV, and return it if found (currently, each new reference<br/>&gt; to a shared variable creates a new proxy, which is what causes<br/>&gt; this mess).<br/>&gt; <br/><br/>I forgot one not-so-minor detail: clone processing<br/>would need to be updated to<br/>detect and replace the fieldhash&#39;d proxies, and<br/>update each SV that invoked the magic dup() method<br/><br/>- Dean<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1291.html Tue, 06 May 2008 14:00:47 +0000 Re: p5p summary: Improving threads::shared ? by Dean Arnold Jerry D. Hedden wrote:<br/>&gt;&gt; Is this a logical approach? If so, is it doable?<br/>&gt; <br/>&gt; If circular references can&#39;t be fully supported in<br/>&gt; threads::shared, then I need to document this in its POD and<br/>&gt; in Thread::Queue&#39;s POD, too. Do you agree?<br/>&gt; <br/><br/>I don&#39;t know that they can&#39;t be &quot;supported&quot;; but they do<br/>need to be explained. I also don&#39;t see how it effects<br/>T::Q: since you always skip over anything thats already<br/>shared, existing circular refs aren&#39;t an issue. Its only<br/>detecting and creating new shared circular refs that<br/>causes a problem, and thats fixed w/ the fieldhash lookup<br/>(which is keyed on the private address, not the shared<br/>version).<br/><br/>For any existing apps (eg, Data::Dumper) that want to<br/>deal with it, they could always fallback to detecting something<br/>as shared and saving its id (ie, the shared interpretter version&#39;s<br/>address) to detect cycles. Not pretty, but effective.<br/><br/>- Dean<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1290.html Mon, 05 May 2008 12:42:58 +0000 Re: p5p summary: Improving threads::shared ? by Dean Arnold Jerry D. Hedden wrote:<br/>&gt; erry D. Hedden wrote:<br/>&gt;&gt; Look at $x:<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt; REF(0x144f8f0)<br/>&gt;&gt;<br/>&gt;&gt; First look at $y:<br/>&gt;&gt; SCALAR(0x1423c70)<br/>&gt;&gt; SCALAR(0x1423ad8)<br/>&gt;&gt; SCALAR(0x14bd968)<br/>&gt;&gt; SCALAR(0x14bd980)<br/>&gt;&gt; SCALAR(0x14bd998)<br/>&gt;&gt; SCALAR(0x14bd9b0)<br/>&gt;&gt;<br/>&gt;&gt; Second look at $y:<br/>&gt;&gt; REF(0x1423c70)<br/>&gt;&gt; REF(0x1423ad8)<br/>&gt;&gt; REF(0x14bd968)<br/>&gt;&gt; REF(0x14bd980)<br/>&gt;&gt; REF(0x14bd998)<br/>&gt;&gt; SCALAR(0x14bd9b0)<br/>&gt;&gt;<br/>&gt;&gt; Seems to me that this is a bug. It shows that<br/>&gt;&gt; threads::shared isn&#39;t detecting that it&#39;s dealing with a<br/>&gt;&gt; circular reference. With each level that the app traverses,<br/>&gt;&gt; threads::shared &quot;unrolls&quot; another version of the shared<br/>&gt;&gt; variable.<br/>&gt; <br/>&gt;&gt; The more I think about it, the more I&#39;m convinced this needs<br/>&gt;&gt; to be fixed. The original variable $x is a circular<br/>&gt;&gt; reference, and it&#39;s finiteness is detectable. The copy made<br/>&gt;&gt; to $y has become an infinitely deep reference with lazy<br/>&gt;&gt; evaluation.<br/>&gt; <br/>&gt; I&#39;ve been looking at how to fix this, but know understanding<br/>&gt; of the internals of threads::shared is weak.<br/>&gt; <br/>&gt; Conceptually, I think it would require keeping a weak<br/>&gt; reference of each private SV associated with a shared SV.<br/>&gt; This would need to be done on a per-thread basis. Then when<br/>&gt; a thread tries to reference a shared SV, a lookup would be<br/>&gt; made to see if a private SV already exists (and still<br/>&gt; exists) for that thread. If so, that is returned (and the<br/>&gt; ref count of the weak ref is incremented?). If not, a new<br/>&gt; private SV is created, and a weak ref to it is appropriately<br/>&gt; stored.<br/>&gt; <br/>&gt; Is this a logical approach? If so, is it doable?<br/>&gt; <br/><br/>By coincidence, I&#39;ve been doing related<br/>work on Thread::Sociable&#39;s STM implementation.<br/>It has to keep a &quot;shadow&quot; proxy of each variable<br/>in order to avoid the same referencing issue<br/>(otherwise, it could create multiple different<br/>transactional versions of the same referenced variable)<br/><br/>For threads::shared, the only solution I can think of is<br/>adding a fieldhash to the thread-private my_ctx structure<br/>keyed by the address of the referent variable&#39;s<br/>shared version. Then each attempt to create a new proxy<br/>would lookup any existing persistent proxy for the shared<br/>SV, and return it if found (currently, each new reference<br/>to a shared variable creates a new proxy, which is what causes<br/>this mess).<br/><br/>It may create refcounting issues; the private proxy<br/>would need to be refcounted every time the shared<br/>variable was refcounted in the same thread. (Alternately,<br/>the thread could refcount only the proxy, and then decrement<br/>the shared versions refcount only when the private proxy&#39;s<br/>refcount dropped to zero)<br/><br/>I also don&#39;t know how it would effect taking ref&#39;s of<br/>shared array/hash elements.<br/><br/>And this will probably slow things down even further.<br/><br/>- Dean<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1289.html Mon, 05 May 2008 12:36:19 +0000 Re: p5p summary: Improving threads::shared ? by Jerry D. Hedden &gt; Is this a logical approach? If so, is it doable?<br/><br/>If circular references can&#39;t be fully supported in<br/>threads::shared, then I need to document this in its POD and<br/>in Thread::Queue&#39;s POD, too. Do you agree?<br/> http://www.nntp.perl.org/group/perl.ithreads/2008/05/msg1288.html Mon, 05 May 2008 12:14:10 +0000