Re: Suspending Threads
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sun, 08 Apr 2007 18:46:13 -0700
On Sun, 08 Apr 2007 17:10:16 -0700, Dave <fairydave@xxxxxxxxxxx> wrote:
[...] From what I can tell both Peter
Ritchies code and mine have the thread determining where and when to begin waiting, so presumably the only difference between them would be
coder preference.
For what it's worth, I don't find that to be the only difference. In particular, the Interrupt method a) requires that you have a reference to the Thread object (not always the case) and b) causes an exception to occur in the thread.
IMHO it's this latter aspect that is the bigger problem. With either of the other two methods (mutex or event), the code simply sits and waits, and then continues normally when signaled. With Interrupt, you need to handle the exception and somehow re-enter the code that should be doing the work.
So while I agree that all three methods can accomplish essentially the same thing, I find that using Sleep/Interrupt is significantly inferior to the other two methods. That is, if the point is simply to introduce a point at which the code pauses and then resumes, Sleep/Interrupt breaks that model by forcing the code to exit the block and re-enter later. It's my opinion that creating a wait object is far less of an issue than forcing the code into a mold that isn't really appropriate.
I think Sleep/Interrupt would be more useful in situations where you don't have an infinite sleep time and in which the normal situation is that the sleep runs for the full time. In that case, using Interrupt to create an exception would make more sense because, well...interrupting the thread would actually be the exceptional case in that situation.
For what it's worth, I also don't see mutex and event as being exactly equivalent, since the event is more a matter of signaling, while the mutex is an ongoing thing. In other words, I would use an event where I simply need to wake a thread up and let it continue processing, and a mutex where I have some resource that needs to be protected while a single thread operates on it.
Pete
.
- Follow-Ups:
- Re: Suspending Threads
- From: Dave
- Re: Suspending Threads
- References:
- Suspending Threads
- From: Dave
- Re: Suspending Threads
- From: Dave
- Re: Suspending Threads
- From: Jon Skeet [C# MVP]
- Re: Suspending Threads
- From: Dave
- Suspending Threads
- Prev by Date: Re: Suspending Threads
- Next by Date: What is the best way to read a HTML "readable" web directory...
- Previous by thread: Re: Suspending Threads
- Next by thread: Re: Suspending Threads
- Index(es):
Relevant Pages
|