Another great example. Thank you very much!<div><br></div><div>I had to evaluate the whole thing inside of a [ ... ] fork, in order to see the updates in the Transcript... <br><br><div class="gmail_quote">2012/11/2 David T. Lewis <span dir="ltr"><<a href="mailto:lewis@mail.msen.com" target="_blank">lewis@mail.msen.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Fri, Nov 02, 2012 at 07:40:08PM +0100, Bert Freudenberg wrote:<br>
> On 2012-11-02, at 19:24, Sebastian Nozzi <<a href="mailto:sebnozzi@gmail.com">sebnozzi@gmail.com</a>> wrote:<br>
><br>
> > Thanks!<br>
> ><br>
> > Didn't know it was so simple... I was kind of intimidated by "wait". Thought, well, that it suspended the thread no matter what.<br>
> ><br>
> > Another question, out of curiosity:<br>
> ><br>
> > Is it possible to have one thread wait for 2 other threads to finish? Would it also be solved with Semaphores like this?<br>
> ><br>
> > s1 := Semaphore forMutualExclusion.<br>
> > s2 := Semaphore forMutualExclusion.<br>
> > [ s1 wait. do stuff .... s1 signal] fork.<br>
> > [ s2 wait. do stuff ..... s2 signal ] fork.<br>
> ><br>
> > s1 wait.<br>
> > s2 wait.<br>
> > "Here we are sure the two other processes finished, kind of..."<br>
> ><br>
> > Or is there a more "friendly" way? :-)<br>
> ><br>
> > Thanks again!<br>
> ><br>
> > Sebastian<br>
><br>
> Looks okay to me. But you could make it a bit simpler:<br>
><br>
> s1 := Semaphore new.<br>
> s2 := Semaphore new.<br>
> [ do stuff .... s1 signal] fork.<br>
> [ do stuff ..... s2 signal] fork.<br>
> s1 wait.<br>
> s2 wait.<br>
><br>
> Or even:<br>
><br>
> s1 := Semaphore new.<br>
> [ do stuff .... s1 signal] fork.<br>
> [ do stuff ..... s1 signal] fork.<br>
> s1 wait; wait.<br>
><br>
<br>
</div></div>And you can see how this works by writing a few things to the transcript.<br>
Try evaluating this in a workspace:<br>
<br>
Transcript clear.<br>
sema := Semaphore new.<br>
arrayOfFourProcesses := {<br>
[(Delay forSeconds: 8) wait. sema signal] fork.<br>
[(Delay forSeconds: 2) wait. sema signal] fork.<br>
[(Delay forSeconds: 6) wait. sema signal] fork.<br>
[(Delay forSeconds: 4) wait. sema signal] fork<br>
}.<br>
(Delay forMilliseconds: 100) wait. "allow the processes to start"<br>
<br>
Transcript cr; show: DateAndTime now asString.<br>
arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].<br>
<br>
sema wait.<br>
Transcript cr; cr; show: DateAndTime now asString, ' one process just finished'.<br>
arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].<br>
<br>
sema wait.<br>
Transcript cr; cr; show: DateAndTime now asString, ' another process just finished'.<br>
arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].<br>
<br>
sema wait.<br>
Transcript cr; cr; show: DateAndTime now asString, ' and another process just finished'.<br>
arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].<br>
<br>
sema wait.<br>
Transcript cr; cr; show: DateAndTime now asString, ' the last process just finished'.<br>
arrayOfFourProcesses do: [:e | Transcript cr; show: e printString].<br>
<br>
Dave<br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
<a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>