Re: thread-running?
Emilio Lopes <eclig <at> gmx.net>
2007-06-05 18:24:47 GMT
Michael Sperber writes:
> Emilio Lopes <eclig <at> gmx.net> writes:
>> Is this a correct definition of `thread-running?'?
>>
>> (define (thread-running? thread)
>> (and (thread-cell thread) #t))
> I'm not sure - what would you use it for?
I want to check if a thread is running from the "user" point of view,
meaning: if the thread is eligible to be scheduled again (i.e. not
finished, terminated or killed).
This is unlike `running?' from `threads-internal', which checks if the
thread is running *now*.
I'm working on an application which starts a "server thread" listening
on some port. At some point in time I want to check if the server
thread for a given port is still running and maybe restart it if it's
not. Maybe I would be better off checking if there's some socket on
the given port, but this doesn't seem possible right now...
> It seems to be a recipe for race conditions. Moreover, the thread
> cell also gets cleared when a thread is scheduled, so it's unlikely
> to correspond to your notion of "running".
My first idea was to check the thread continuation, since it's #f after
the thread is finished. But I then noticed that terminated/killed
threads also have non-#f continuations.
I've came to the following definition, but after some quick tests I
thought that checking the cell was enough. After reading your
explanation I see it's not.
(define (thread-running? thread)
(and (thread-continuation thread) ; finished?
(let ((events (thread-events thread)))
(not (and events
(on-queue? events
(enum event-type killed))))) ; killed?
(thread-cell thread) ; terminated?
#t))
Is this OK?
>> I noticed a delay between killing/terminating a thread and the above
>> definition of `thread-running?' returning #f. Why is that? (I
>> couldn't completely understand the code in thread.scm)
> You should only see a delay when killing - killing just sends an event
> to the thread, instructing it to quit at its own leisure.
OK, I see. Thanks for explaining.
--
--
Emílio C. Lopes Ich leb und weiß nit wie lang,
Munich, Germany ich stirb und weiß nit wann,
ich fahr und weiß nit wohin,
(Martinus von Biberach) mich wundert, dass ich fröhlich bin!