[Newbies] Simple semaphore to synchronize instances of different classes

patrick dudjalija patrick.dudjalija at gmail.com
Tue Aug 14 19:24:06 UTC 2012


Hi everybody !

I'm developing an educative game.
I've written a "gameEngine" class which does the logic for the general
progression in the game : switching between instances of morph
subclasses which implement intro screen / mini-games/levels / end
screen etc ...

Now, i need to synchronize the instance of the gameEngine class with
the instances of the other classes.
Example : once the gameEngine starts the instance of a morph subclass
which implements a mini-game, it must sleep until the mini-game is
finished, and then wake up and does further required switching (next
mini-game or intro screen or end screen ...).

i thought that a "simple" semaphore would quickly give a solution to
the problem.
But i get a freezed code.

I've modelized/simplified the problem with the following workspace code.
See also fileout of Class TestSynchro as attachment.
TestSynchro has a step method which prints some lines to Transcript,
then calls the method stopStepping and signal to the semaphore.
The workspace code does a wait, before printing to Transcript 'Hello World'!

Problem is that the code freezes , on the line code -> sem wait
I first thought that the code freezed because in the same process. And
added a fork to the block which creates instance of TestSynchro. But
it does'nt help ...

Any help will be appreciated !
Thanks by advance,

Patrick Dudjalija



|ts sem|

sem:=Semaphore new.

[ts:= TestSynchro new.
ts semaphore:sem.
ts openInWorld.
ts start.] fork.

sem wait.
Transcript cr; show:'Hello world !';cr.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TestSynchro.st
Type: application/octet-stream
Size: 2201 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/beginners/attachments/20120814/8d30cdd2/TestSynchro.obj


More information about the Beginners mailing list