Hi,
I've been spending some time improving my poor understanding of processes in Squeak.
After studying through the usual suspects (essentially the bluebook and X.Briffault french book, I've not used yet any VW focused book, to avoid confusion) and some google-ing, I am trying to find clear examples with almost-trivial behavior in order to illustrate the main points of squeak concurrency (I want to teach it, fortunately that will not happen anytime soon).
Well, I need to re-define "almost trivial". Let's see. I thought that the following code (executed from a Workspace):
| s p1 p2 | s := SharedQueue new: 20. p1 := [(1 to: 10) do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [(11 to: 20) do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
was almost-trivial. It behaves as one would expect: s contains #(1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20)
But, if you agree with me in the "almost-triviality" of the example, probably you should be able to explain why the following code:
| s p1 p2 | s := SharedQueue new: 20. p1 := [1 to: 10 do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [11 to: 20 do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
ends up with s containing: #(1 11 13 14 15 16 17 18 19 20 nil nil nil nil nil nil nil nil nil nil)
(notice that the only difference is that I am sending a to:do: message to a SmallInteger, instead of a do: message to an Interval).
So, as of now, I am unable to figure out what is going on.
Any clue? please? I'd rather prefer a sophisticated answer, otherwise I'm going to feel like an idiot :-))
Thanks a lot in advance,
Bests,
Jordi
PS: This behavior is identical in Squeak 3.9, 3.10 and Pharo, and in a Linux VM and a Mac VM.
On Mon, Mar 30, 2009 at 5:03 PM, Jordi Delgado jdelgado@lsi.upc.edu wrote:
| s p1 p2 | s := SharedQueue new: 20. p1 := [1 to: 10 do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [11 to: 20 do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
Try to rename your block variables i to i1 and i2. You can also try sending the message #fixTemps to the blocks.
On 30.03.2009, at 17:13, Damien Cassou wrote:
On Mon, Mar 30, 2009 at 5:03 PM, Jordi Delgado jdelgado@lsi.upc.edu wrote:
| s p1 p2 | s := SharedQueue new: 20. p1 := [1 to: 10 do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [11 to: 20 do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
Try to rename your block variables i to i1 and i2. You can also try sending the message #fixTemps to the blocks.
Right. The compiler rewrites the #to:do: loop into a #whileTrue: loop and uses the same method-level temp "i" for both loops. This issue (among others) will be fixed with the "closure compiler".
- Bert -
Your problem comes from a compiler issue that is fixed with the new closure support from Eliot. If you run your example in a closure converted image[1] using a very recent VM [2] it will work as you expect.
[1] http://squeakvm.org/win32/release/Squeak-3.10.2-Closures.zip [2] http://squeakvm.org/win32/release/SqueakVM-Win32-3.11.2-bin.zip
Cheers, - Andreas
Jordi Delgado wrote:
Hi,
I've been spending some time improving my poor understanding of processes in Squeak.
After studying through the usual suspects (essentially the bluebook and X.Briffault french book, I've not used yet any VW focused book, to avoid confusion) and some google-ing, I am trying to find clear examples with almost-trivial behavior in order to illustrate the main points of squeak concurrency (I want to teach it, fortunately that will not happen anytime soon).
Well, I need to re-define "almost trivial". Let's see. I thought that the following code (executed from a Workspace):
| s p1 p2 | s := SharedQueue new: 20. p1 := [(1 to: 10) do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [(11 to: 20) do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
was almost-trivial. It behaves as one would expect: s contains #(1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20)
But, if you agree with me in the "almost-triviality" of the example, probably you should be able to explain why the following code:
| s p1 p2 | s := SharedQueue new: 20. p1 := [1 to: 10 do: [:i | s nextPut: i. Processor yield.]] newProcess. p2 := [11 to: 20 do: [:i | s nextPut: i. Processor yield.]] newProcess. p1 priority: Processor activePriority - 1. p2 priority: Processor activePriority - 1. p1 resume. p2 resume. s inspect.
ends up with s containing: #(1 11 13 14 15 16 17 18 19 20 nil nil nil nil nil nil nil nil nil nil)
(notice that the only difference is that I am sending a to:do: message to a SmallInteger, instead of a do: message to an Interval).
So, as of now, I am unable to figure out what is going on.
Any clue? please? I'd rather prefer a sophisticated answer, otherwise I'm going to feel like an idiot :-))
Thanks a lot in advance,
Bests,
Jordi
PS: This behavior is identical in Squeak 3.9, 3.10 and Pharo, and in a Linux VM and a Mac VM.
On 30-Mar-09, at 8:31 AM, Andreas Raab wrote:
Your problem comes from a compiler issue that is fixed with the new closure support from Eliot. If you run your example in a closure converted image[1] using a very recent VM [2] it will work as you expect.
[1] http://squeakvm.org/win32/release/Squeak-3.10.2-Closures.zip [2] http://squeakvm.org/win32/release/SqueakVM-Win32-3.11.2-bin.zip
Cheers,
- Andreas
And a mac VM 4.0.0beta1U which support Closures found via the links at http://smalltalkconsulting.com/squeak.html
-- = = = ======================================================================== John M. McIntosh johnmci@smalltalkconsulting.com Corporate Smalltalk Consulting Ltd. http://www.smalltalkconsulting.com = = = ========================================================================
squeak-dev@lists.squeakfoundation.org