Unexpected result with async process execution

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected result with async process execution

Bernhard Kohlhaas-2
Hello,

With DVE 5.1.3 I am using an async thread and feeding the result into
the main input queue via the #postToInputQueue method. However the
result is not, what I am expecting. And since I'm sure that it is
a case of misplaced expectations, perhaps someone could explain me,
what is going wrong.

My example class file out is quoted below.

When I execute "f := foo new start" in a Workspace and
afterwards evaluate "f events" the result is
"an OrderedCollection(3 3 3)".

However I would have expected "an OrderedCollection(1 2 3)"
as a result. It seems that the 2nd time the block in
#asyncThread" is executed, the exact same "object reference" is
used (and changed) every time for variable "event"
I guess that I don't understand blocks that well after all
and I couldn't find too much abou this in my "Smalltalk-80" copy either.

So does that mean that I can't use local variables in a block like this?
Is there a solution that would bring the expected result?

As always any help is appreciated,

Bernhard

=================== Begin of fileout ===================
Object subclass: #Foo
        instanceVariableNames: 'events'
        classVariableNames: ''
        poolDictionaries: ''
        classInstanceVariableNames: ''!
Foo guid: (GUID fromString: '{C3CB1151-DF44-42E5-9D2C-26AC06222BE5}')!
Foo comment: ''!
!Foo categoriesForClass!Unclassified! !
!Foo methodsFor!

asyncThread
       
        | counter |
        counter := 0.
        3 timesRepeat:  [  | event |
                counter := counter + 1.
                event := counter.
                [ self receiveEvents: event ] postToInputQueue ].!

events
        ^events!

receiveEvents: anEvent

        events add: anEvent.
!

start

        events := OrderedCollection new.
        [ self asyncThread ] fork.! !
!Foo categoriesFor: #asyncThread!private! !
!Foo categoriesFor: #events!public! !
!Foo categoriesFor: #receiveEvents:!private! !
!Foo categoriesFor: #start!public! !
============ End of fileout =========================


--
(This email address is only temporarily valid, due to the heavy amount
of spam a post generates. For a permanently valid email address, just
remove all numerical digits from the address.)


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected result with async process execution

Carsten Haerle
This phenomen is because Dolphin does not has real block closures currently.
Dolphin 6 is going to change this. Until then you can extract the block
posted to the input queue into a separate method and everything should work
fine:

blockToBePostedWithEvent: event
^[ self receiveEvents: event ]

and change to code to read:

asyncThread
| counter |
counter := 0.
3 timesRepeat:  [  | event |
counter := counter + 1.
event := counter.
(self blockToBePostedWithEvent: event) postToInputQueue ].!

Regards

Carsten

"Bernhard Kohlhaas" <[hidden email]> schrieb im
Newsbeitrag news:[hidden email]...

> Hello,
>
> With DVE 5.1.3 I am using an async thread and feeding the result into
> the main input queue via the #postToInputQueue method. However the
> result is not, what I am expecting. And since I'm sure that it is
> a case of misplaced expectations, perhaps someone could explain me,
> what is going wrong.
>
> My example class file out is quoted below.
>
> When I execute "f := foo new start" in a Workspace and
> afterwards evaluate "f events" the result is
> "an OrderedCollection(3 3 3)".
>
> However I would have expected "an OrderedCollection(1 2 3)"
> as a result. It seems that the 2nd time the block in
> #asyncThread" is executed, the exact same "object reference" is
> used (and changed) every time for variable "event"
> I guess that I don't understand blocks that well after all
> and I couldn't find too much abou this in my "Smalltalk-80" copy either.
>
> So does that mean that I can't use local variables in a block like this?
> Is there a solution that would bring the expected result?
>
> As always any help is appreciated,
>
> Bernhard
>
> =================== Begin of fileout ===================
> Object subclass: #Foo
> instanceVariableNames: 'events'
> classVariableNames: ''
> poolDictionaries: ''
> classInstanceVariableNames: ''!
> Foo guid: (GUID fromString: '{C3CB1151-DF44-42E5-9D2C-26AC06222BE5}')!
> Foo comment: ''!
> !Foo categoriesForClass!Unclassified! !
> !Foo methodsFor!
>
> asyncThread
>
> | counter |
> counter := 0.
> 3 timesRepeat:  [  | event |
> counter := counter + 1.
> event := counter.
> [ self receiveEvents: event ] postToInputQueue ].!
>
> events
> ^events!
>
> receiveEvents: anEvent
>
> events add: anEvent.
> !
>
> start
>
> events := OrderedCollection new.
> [ self asyncThread ] fork.! !
> !Foo categoriesFor: #asyncThread!private! !
> !Foo categoriesFor: #events!public! !
> !Foo categoriesFor: #receiveEvents:!private! !
> !Foo categoriesFor: #start!public! !
> ============ End of fileout =========================
>
>
> --
> (This email address is only temporarily valid, due to the heavy amount
> of spam a post generates. For a permanently valid email address, just
> remove all numerical digits from the address.)
>


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected result with async process execution

Bernhard Kohlhaas-2
Using a new method did the trick. Thanks so much.

Bernhard

Carsten Haerle wrote:
> This phenomen is because Dolphin does not has real block closures currently.
> Dolphin 6 is going to change this. Until then you can extract the block
> posted to the input queue into a separate method and everything should work
> fine:
[...]

--
(This email address is only temporarily valid, due to the heavy amount
of spam a post generates. For a permanently valid email address, just
remove all numerical digits from the address.)