roxen.lists.pike.general

Subject Author Date
Re: thread and lambda PeterPan <zenothing[at]hotmail[dot]com> 22-03-2009
> 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.

When the programmer realized the mistake, he may change his code this way:

for(int i=0;i<100;i++){
    Thread.Thread(Function.curry(lambda(int i){ 
sleep(3);write("%d\n",i);})(i));
}