Search code examples
multithreadingconcurrencysmalltalkvisualworks

Smalltalk Visual Works concurrency


With the follwing code, I expect an output like

AA
BB
AA
AA
AA
BB

Code:

p1 := [ 1 to: 3000 do: [:i | Transcript show: 'AA';cr.] ] newProcess.
p2 := [ 5000 to: 8100 do: [:i | Transcript show: 'BB';cr.] ] newProcess. 
p1 resume.
p2 resume.

But it first prints all the AAs and then all the BBs. Works fine under Pharo, but not VisualWorks. Can someone tell the bug?


Solution

  • VisualWorks uses non-preemptive multitasking This means that when two processes have the same priority, one process needs to yield in order to another to run. Try running the following code to see the difference:

    p1 := [ 1 to: 3000 do: [:i |
        Transcript show: 'AA';cr.
        i \\ 10 = 0 ifTrue: [Processor activeProcess yield]] ] newProcess.
    p2 := [ 5001 to: 8101 do: [:i |
        Transcript show: 'BB';cr.
        i \\ 10 = 0 ifTrue: [Processor activeProcess yield]] ] newProcess. 
    p1 resume.
    p2 resume.