[Newbies] Acquiring lock, releasing in callback

Bert Freudenberg bert at freudenbergs.de
Fri Nov 2 18:40:08 UTC 2012


On 2012-11-02, at 19:24, Sebastian Nozzi <sebnozzi at 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.

- Bert -

> 
>> You could use a Semaphore directly:
>> 
>> 	semaphore := Semaphore forMutualExclusion.
>> 
>> and then
>> 
>> 	semaphore wait "acquire lock"
>> 	...
>> 	semaphore signal "release lock"
>> 
>>> I am aware or Monitor/Mutex critical:, but I am not sure I can use it in my case.
>> 
>> 
>> Semaphore>>critical: simply does a wait and signal like above. But it ensures that if something goes wrong the semaphore is still signaled, so it's more easy to use.
>> 
>> - Bert -





More information about the Beginners mailing list