On Fri, Nov 02, 2012 at 07:40:08PM +0100, Bert Freudenberg wrote:
On 2012-11-02, at 19:24, Sebastian Nozzi sebnozzi@gmail.com wrote:
Thanks!
Didn't know it was so simple... I was kind of intimidated by "wait". Thought, well, that it suspended the thread no matter what.
Another question, out of curiosity:
Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this?
s1 := Semaphore forMutualExclusion. s2 := Semaphore forMutualExclusion. [ s1 wait. do stuff .... s1 signal] fork. [ s2 wait. do stuff ..... s2 signal ] fork.
s1 wait. s2 wait. "Here we are sure the two other processes finished, kind of..."
Or is there a more "friendly" way? :-)
Thanks again!
Sebastian
Looks okay to me. But you could make it a bit simpler:
s1 := Semaphore new. s2 := Semaphore new. [ do stuff .... s1 signal] fork. [ do stuff ..... s2 signal] fork. s1 wait. s2 wait.
Or even:
s1 := Semaphore new. [ do stuff .... s1 signal] fork. [ do stuff ..... s1 signal] fork. s1 wait; wait.
And you can see how this works by writing a few things to the transcript. Try evaluating this in a workspace:
Transcript clear. sema := Semaphore new. arrayOfFourProcesses := { [(Delay forSeconds: 8) wait. sema signal] fork. [(Delay forSeconds: 2) wait. sema signal] fork. [(Delay forSeconds: 6) wait. sema signal] fork. [(Delay forSeconds: 4) wait. sema signal] fork }. (Delay forMilliseconds: 100) wait. "allow the processes to start"
Transcript cr; show: DateAndTime now asString. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].
sema wait. Transcript cr; cr; show: DateAndTime now asString, ' one process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].
sema wait. Transcript cr; cr; show: DateAndTime now asString, ' another process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].
sema wait. Transcript cr; cr; show: DateAndTime now asString, ' and another process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].
sema wait. Transcript cr; cr; show: DateAndTime now asString, ' the last process just finished'. arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].
Dave