[Newbies] Acquiring lock, releasing in callback

Bert Freudenberg bert at freudenbergs.de
Fri Nov 2 18:06:50 UTC 2012

On 2012-11-02, at 18:42, Sebastian Nozzi <sebnozzi at gmail.com> wrote:

> Hello Dear Squeakers,
> threads and co. are giving me headaches :-) I wanted to ask for advice...
> What do I use in Squeak to prevent more than one thread entering a region, but "releasing" the exclusivity in another block/callback? For example, let's suppose there is an hypothetical class "Lock" that does that.
> lock acquire.
> longOperation performWhenFinished: [ callback code ... lock release ].
> ...
> rest of the program
> ...
> Is there something like that? Or do I have to simulate it with something else? (Semaphore?)

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