roxen.lists.pike.general

Subject Author Date
Re: thread and lambda zenothing <zenothing[at]hotmail[dot]com> 22-03-2009
Mirar:

Do you remember this bug: 
http:<//www.mail-archive.com/pike-devel[at]lists.lysator.liu.se>/msg00802.html

_typeof Bug:

void main()
{
        mapping m=([]);
        mixed a=lambda(string s){ string k,b; sscanf(s,"%s=%s",k,b);
m[k]=b;};
        mixed b=lambda(string s){ string k,b; sscanf(s,"%s=%s",k,b);};
        write("%O\n",_typeof(a));
        write("%O\n",_typeof(b));
}

$ pike t.pike
function
function(string : zero)
^--------not same!? They should be same I think.


My proposal is Thread.SafeThread(b) be OK; Thread.SafeThread(a) give the 
WARNING.


--------------------------------------------------
From: "Mirar @ Pike  importm?te f?r mailinglistan" 
<<6341[at]lyskom.lysator.liu.se>>
Sent: Sunday, March 22, 2009 8:45 PM
To: <<pike[at]roxen.com>>
Subject: Re: thread and lambda

> >int sum;
> >for(int i=0;i<100;i++){
> >    Thread.Thread(lambda(){
> >        /* too much code here, and take a long time to run */
> >        write("%d\n",i);
> >        sum+=i;
> >        /* too much code here too*/
> >    });
> >}
>
> So, the problem is that you're using variables you thought were local,
> but they aren't?
Yes, I'm trying to avoid this.
>
> >My problem is not "how to write correct code" but "how to avoid mistake"
>
> But I don't see how your proposal would solve that problem.
>
> All non-local variables would be thread-shared if you're using threads
> anywhere in the environment. A lambda isn't special. I'm not sure
> where you would want the warning - in the use of lambdas with
> out-of-scope variables that are given as argument to Thread.Thread()?
>
> Should this give a warning to? Where?
>
>    int global_variable;
>    function f=lambda() { global_variable++; }
>    void myfunc() { Thread.Thread(f); }
>
Give the warning! maybe we need a new class Thread.SafeThread to do this.

> this?
>
>    function g() { global_variable++; };
>    function f=g;
>    void myfunc() { Thread.Thread(f); }
Give the warning too!

>
> And locks really doesn't have anything to do with the problem of
> variables changing value outside the thread:
>
>   Thread.Mutex i_lock=Thread.Mutex();
>   object key=i_lock()->lock;
>   for(int i=0;i<100;i++){
>       Thread.Thread(lambda(){
>           object key=i_lock()->lock;
>           ...i...
>           destruct(key);
>       })};
>   destruct(key);
>
> or even
>
>   Thread.Mutex i_lock=Thread.Mutex();
>   for (int i=0; i<100; )
>   {
>       Thread.Thread(lambda(){
>           object key=i_lock()->lock;
>           ...i...
>           destruct(key);
>       });
>       object key=i_lock()->lock;
>       i++;
>       destruct(key);
>   }
>
> seems to solve the problem you mention.

If the programmer not confused, he can do whatever he need to work out the 
correct code. Let's leave the work to him.

>
>
> Sorry if I'm confused...
>
>