#fork and deterministic resumption of the resulting process
Joshua Gargus
schwa at fastmail.us
Tue Feb 5 16:33:51 UTC 2008
On Feb 4, 2008, at 6:21 PM, Igor Stasenko wrote:
> On 04/02/2008, Andreas Raab <andreas.raab at gmx.de> wrote:
>> Hi -
>>
>> In my never-ending quest for questionable behavior in multi-threaded
>> situations just today I ran into a pattern which is dangerously
>> common
>> in our code. It basically goes like this:
>>
>
> Hmm, IMO, you wanting to kill two rabbits in one shot..
>
> Why not write like following:
>
> MyClass>>startWorkerProcess
> "worker is an instance variable"
> running := true.
> worker := [self runWorkerProcess] fork.
>
> MyClass>>runWorkerProcess
> "Run the worker process"
> [running] whileTrue:[
> "...do the work..."
> ].
>
> MyClass>>stopWorkerProcess
> "Stop the worker process"
> running := false. "let it terminate itself"
This doesn't work either (and is actually the reason that the
original, broken pattern described by Andreas was used). Consider the
following:
inst := MyClass new.
inst startWorkerProcess; stopWorkerProcess; startWorkerProcess
If the first worker process happens not to notice that 'running' was
set to false for a moment, then you will have two processes running.
By comparing against the worker process, you avoid this problem.
Josh
>
>
> Yes, you will need an additional inst var - 'running', but i think
> it's reasonable: controlling a process in context of scheduler
> operations, where you need it's handle, and controlling when it's
> should terminate graciously (by setting running flag to false), is
> different things.
>
> --
> Best regards,
> Igor Stasenko AKA sig.
>
More information about the Squeak-dev
mailing list
|