Look at forMutualExclusion implementation, and you'll see it's set up with one excess signal.
So the first thing to wait for it, doesn't have to wait, ie. your
semaphores do:[]... falls straight through and 'all forks processed' gets displayed before any of the forks are run.
Substitute with new, and you SHOULD get the behaviour you expect.
However, show: is NOT threadsafe, it releases access lock between nextPutAll and endEntry, thus you often end up with several threads writing to stream, then endEntry displaying them multiple times If you change nextPutAll: back to show: in the code below, this is apparent (at least it was on my machine...)

|semaphores tr|
semaphores := Array new: 10.
tr := ThreadSafeTranscript new.
tr open.
1 to: 10 do: [ :index | semaphores at: index put: Semaphore new ].

    1 to: 10 do: [:i |
        [
        tr nextPutAll: i printString, ' fork '; cr.
        (semaphores at: i) signal.
        ] fork
    ].

    semaphores do: [:each | each wait ].
    tr show: 'all forks proccesed'; cr.

Cheers,
Henry


On 01.07.2009 16:41, Mariano Martinez Peck wrote:
Hi folks! I am doing some benchmarks and I having problems with fork (I think).

I have this method collect:

collect
|semaphores tr|
semaphores := Array new: 10.
tr := ThreadSafeTranscript new.
tr open.
1 to: 10 do: [ :index | semaphores at: index put: Semaphore forMutualExclusion ].

    1 to: 10 do: [:i |
        [
        tr show: 'one fork'; cr.
        (semaphores at: i) signal.
        ] fork
    ].

    semaphores do: [:each | each wait ].
    tr show: 'all forks proccesed'; cr.


What I want is that the method collect returns when ALL of the forks created inside, are finished. Obviously in my real code, I don't do a transcript show: but another thing.

I would expect something like this:

one fork
one fork
one fork
one fork
one fork
one fork
one fork
one fork
one fork
one fork
all forks proccesed

But the output is:


all forks proccesed
one fork
one fork
one fork
one forkone fork
one fork
one fork
one forkone fork
one fork
one fork
one forkone fork

So, I guess it isn't working :( I don't know too much about forks so any help is welcome!

can be a problem with ThreadSafeTranscript ? If so, how could I test if my code is working as expected ?

Thanks,

Mariano

------------------------------------------------------------------------

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to