[Pharo-users] Semaphore wait and signal

stepharo stepharo at free.fr
Thu May 12 06:20:58 EDT 2016


Superb email ben

We should really push a little tutorial on concurrent programming with 
Pharo :)

Stef


Le 11/5/16 à 15:08, Ben Coman a écrit :
> On Tue, May 10, 2016 at 6:31 PM, Vince Refiti <vinref at gmail.com> wrote:
>> Hello
>>
>> I am playing around with Semaphore, and wrote the following:
> [snip]
>
>> Can someone please explain this pattern?
> I rearranged again to make the output self explanatory...
>
>    | coll count sem |
>    Transcript clear; show: 'count' ; tab; show: 'each'.
>    coll := Array withAll: (1 to: 99).
>    count := 0.
>    sem := Semaphore new.
>    coll do:
>    [   :each |
>        count := count + 1.
>        Transcript crShow: count printString; tab.
>        (count >= 5) ifTrue:
>            [ sem isSignaled
>                 ifTrue: [
>                     Transcript tab; show: 'consumeExcess, nowait, '.
>                      sem wait ]
>                 ifFalse: [
>                    Transcript tab; show: 'main/UI waits on sem'.
>                     sem wait.
>                     Transcript cr; tab; tab;
>                            show: 'main/UI runs, excessSignals ' ;
>                            show: sem excessSignals printString; cr; tab. ]
>             ].
>
>       Transcript tab ; show: each printString, ' queued to run'.
>        [    count := count -1.
>             Transcript crShow: count printString; tab ; tab;
>                        show: each printString,  ' runs, signals sem'.
>              sem signal.
>              sem isSignaled ifFalse: [  Transcript show: ', UI queued to run' ].
>        ] fork.
>    ]
>
>
>
> On Tue, May 10, 2016 at 6:31 PM, Vince Refiti <vinref at gmail.com> wrote:
>> Hello
>>
>> I am playing around with Semaphore, and wrote the following:
> [snip]
>
>> Can someone please explain this pattern?
> I rearranged again to make the output self explanatory...
>
>    | coll count sem |
>    Transcript clear; show: 'count' ; tab; show: 'each'.
>    coll := Array withAll: (1 to: 99).
>    count := 0.
>    sem := Semaphore new.
>    coll do:
>    [   :each |
>        count := count + 1.
>        Transcript crShow: count printString; tab.
>        (count >= 5) ifTrue:
>            [ sem isSignaled
>                 ifTrue: [
>                     Transcript tab; show: 'nowait '.
>                      sem wait ]
>                 ifFalse: [
>                    Transcript tab; show: 'main/UI waits on sem'.
>                     sem wait.
>                     Transcript cr; tab; tab;
>                            show: 'main/UI runs, excessSignals ' ;
>                            show: sem excessSignals printString; cr; tab. ]
>             ].
>
>       Transcript tab ; show: each printString, ' queued to run'.
>        [    count := count -1.
>             Transcript crShow: count printString; tab ; tab;
>                        show: each printString,  ' runs, signals sem'.
>              sem signal.
>              sem isSignaled ifFalse: [  Transcript show: ', main/UI
> queued to run' ].
>        ] fork.
>    ]
>
>
> count each
> 1    1 queued to run
> 2    2 queued to run
> 3    3 queued to run
> 4    4 queued to run
> 5    main/UI waits on sem
> 4    1 runs, signals sem, main/UI queued to run
> 3    2 runs, signals sem
> 2    3 runs, signals sem
> 1    4 runs, signals sem
>       main/UI runs, excessSignals 3
>       5 queued to run
> 2    6 queued to run
> 3    7 queued to run
> 4    8 queued to run
> 5    consumeExcess, nowait, 9 queued to run
> 6    consumeExcess, nowait, 10 queued to run
> 7    consumeExcess, nowait, 11 queued to run
> 8    main/UI waits on sem
> 7    5 runs, signals sem, main/UI queued to run
> 6    6 runs, signals sem
> 5    7 runs, signals sem
> 4    8 runs, signals sem
> 3    9 runs, signals sem
> 2    10 runs, signals sem
> 1    11 runs, signals sem
>       main/UI runs, excessSignals 6
>       12 queued to run
> 2    13 queued to run
> 3    14 queued to run
> 4    15 queued to run
> 5    consumeExcess, nowait, 16 queued to run
> 6    consumeExcess, nowait, 17 queued to run
> 7    consumeExcess, nowait, 18 queued to run
> 8    consumeExcess, nowait, 19 queued to run
> 9    consumeExcess, nowait, 20 queued to run
> 10   consumeExcess, nowait, 21 queued to run
> 11   main/UI waits on sem
> 10   12 runs, signals sem, main/UI queued to run
> 9    13 runs, signals sem
> 8    14 runs, signals sem
> 7    15 runs, signals sem
> 6    16 runs, signals sem
> 5    17 runs, signals sem
> 4    18 runs, signals sem
> 3    19 runs, signals sem
> 2    20 runs, signals sem
> 1    21 runs, signals sem
>       main/UI runs, excessSignals 9
>       22 queued to run
> 2    23 queued to run
> 3    24 queued to run
> 4    25 queued to run
> 5    consumeExcess, nowait, 26 queued to run
> 6    consumeExcess, nowait, 27 queued to run
> 7    consumeExcess, nowait, 28 queued to run
> 8    consumeExcess, nowait, 29 queued to run
> 9    consumeExcess, nowait, 30 queued to run
> 10   consumeExcess, nowait, 31 queued to run
> 11   consumeExcess, nowait, 32 queued to run
> 12   consumeExcess, nowait, 33 queued to run
> 13   consumeExcess, nowait, 34 queued to run
> 14   main/UI waits on sem
> 13   22 runs, signals sem, main/UI queued to run
> 12   23 runs, signals sem
> 11   24 runs, signals sem
> 10   25 runs, signals sem
> 9    26 runs, signals sem
> 8    27 runs, signals sem
> 7    28 runs, signals sem
> 6    29 runs, signals sem
> 5    30 runs, signals sem
> 4    31 runs, signals sem
> 3    32 runs, signals sem
> 2    33 runs, signals sem
> 1    34 runs, signals sem
>       main/UI runs, excessSignals 12
>       35 queued to run
> 2    36 queued to run
> 3    37 queued to run
> 4    38 queued to run
> 5    consumeExcess, nowait, 39 queued to run
> 6    consumeExcess, nowait, 40 queued to run
> 7    consumeExcess, nowait, 41 queued to run
> 8    consumeExcess, nowait, 42 queued to run
> 9    consumeExcess, nowait, 43 queued to run
> 10   consumeExcess, nowait, 44 queued to run
> 11   consumeExcess, nowait, 45 queued to run
> 12   consumeExcess, nowait, 46 queued to run
> 13   consumeExcess, nowait, 47 queued to run
> 14   consumeExcess, nowait, 48 queued to run
> 15   consumeExcess, nowait, 49 queued to run
> 16   consumeExcess, nowait, 50 queued to run
> 17   main/UI waits on sem
> 16   35 runs, signals sem, main/UI queued to run
> 15   36 runs, signals sem
> 14   37 runs, signals sem
> 13   38 runs, signals sem
> 12   39 runs, signals sem
> 11   40 runs, signals sem
> 10   41 runs, signals sem
> 9    42 runs, signals sem
> 8    43 runs, signals sem
> 7    44 runs, signals sem
> 6    45 runs, signals sem
> 5    46 runs, signals sem
> 4    47 runs, signals sem
> 3    48 runs, signals sem
> 2    49 runs, signals sem
> 1    50 runs, signals sem
>       main/UI runs, excessSignals 15
>       51 queued to run
> 2    52 queued to run
> 3    53 queued to run
> 4    54 queued to run
> 5    consumeExcess, nowait, 55 queued to run
> 6    consumeExcess, nowait, 56 queued to run
> 7    consumeExcess, nowait, 57 queued to run
> 8    consumeExcess, nowait, 58 queued to run
> 9    consumeExcess, nowait, 59 queued to run
> 10   consumeExcess, nowait, 60 queued to run
> 11   consumeExcess, nowait, 61 queued to run
> 12   consumeExcess, nowait, 62 queued to run
> 13   consumeExcess, nowait, 63 queued to run
> 14   consumeExcess, nowait, 64 queued to run
> 15   consumeExcess, nowait, 65 queued to run
> 16   consumeExcess, nowait, 66 queued to run
> 17   consumeExcess, nowait, 67 queued to run
> 18   consumeExcess, nowait, 68 queued to run
> 19   consumeExcess, nowait, 69 queued to run
> 20   main/UI waits on sem
> 19   51 runs, signals sem, main/UI queued to run
> 18   52 runs, signals sem
> 17   53 runs, signals sem
> 16   54 runs, signals sem
> 15   55 runs, signals sem
> 14   56 runs, signals sem
> 13   57 runs, signals sem
> 12   58 runs, signals sem
> 11   59 runs, signals sem
> 10   60 runs, signals sem
> 9    61 runs, signals sem
> 8    62 runs, signals sem
> 7    63 runs, signals sem
> 6    64 runs, signals sem
> 5    65 runs, signals sem
> 4    66 runs, signals sem
> 3    67 runs, signals sem
> 2    68 runs, signals sem
> 1    69 runs, signals sem
>       main/UI runs, excessSignals 18
>       70 queued to run
> 2    71 queued to run
> 3    72 queued to run
> 4    73 queued to run
> 5    consumeExcess, nowait, 74 queued to run
> 6    consumeExcess, nowait, 75 queued to run
> 7    consumeExcess, nowait, 76 queued to run
> 8    consumeExcess, nowait, 77 queued to run
> 9    consumeExcess, nowait, 78 queued to run
> 10   consumeExcess, nowait, 79 queued to run
> 11   consumeExcess, nowait, 80 queued to run
> 12   consumeExcess, nowait, 81 queued to run
> 13   consumeExcess, nowait, 82 queued to run
> 14   consumeExcess, nowait, 83 queued to run
> 15   consumeExcess, nowait, 84 queued to run
> 16   consumeExcess, nowait, 85 queued to run
> 17   consumeExcess, nowait, 86 queued to run
> 18   consumeExcess, nowait, 87 queued to run
> 19   consumeExcess, nowait, 88 queued to run
> 20   consumeExcess, nowait, 89 queued to run
> 21   consumeExcess, nowait, 90 queued to run
> 22   consumeExcess, nowait, 91 queued to run
> 23   main/UI waits on sem
> 22   70 runs, signals sem, main/UI queued to run
> 21   71 runs, signals sem
> 20   72 runs, signals sem
> 19   73 runs, signals sem
> 18   74 runs, signals sem
> 17   75 runs, signals sem
> 16   76 runs, signals sem
> 15   77 runs, signals sem
> 14   78 runs, signals sem
> 13   79 runs, signals sem
> 12   80 runs, signals sem
> 11   81 runs, signals sem
> 10   82 runs, signals sem
> 9    83 runs, signals sem
> 8    84 runs, signals sem
> 7    85 runs, signals sem
> 6    86 runs, signals sem
> 5    87 runs, signals sem
> 4    88 runs, signals sem
> 3    89 runs, signals sem
> 2    90 runs, signals sem
> 1    91 runs, signals sem
>       main/UI runs, excessSignals 21
>       92 queued to run
> 2    93 queued to run
> 3    94 queued to run
> 4    95 queued to run
> 5    consumeExcess, nowait, 96 queued to run
> 6    consumeExcess, nowait, 97 queued to run
> 7    consumeExcess, nowait, 98 queued to run
> 8    consumeExcess, nowait, 99 queued to run
> 7    92 runs, signals sem
> 6    93 runs, signals sem
> 5    94 runs, signals sem
> 4    95 runs, signals sem
> 3    96 runs, signals sem
> 2    97 runs, signals sem
> 1    98 runs, signals sem
> 0    99 runs, signals sem
>
> HTH,
> cheers -ben
>
>





More information about the Pharo-users mailing list