TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_|\|_0§P@|/
\|titi____AThotmail.com|/\|@P§0_|\|_>> wrote:
[...]
I don't really agree with that. It is one of the known and desirable effects
of Monitor.Enter and Exit.
[Jon]
But it only makes sense if you understand memory barriers to start
with.
[TT]
True. Maybe memory barriers need
? Interrupt is a necessity if you wait or join ... I don't know what kind of
threads you write,but there are alot of waits in my program and I don't see
how I would stop them from waiting without an interrupt actually.
[J]
With a pulse, for a call to Wait. That's exactly what pulse is for. I
rarely use Thread.Join, but if I was considering the possibility of the
thread hanging, I'd probably use one of the versions that took a
timeout.
[TT]
One small example of some part in my code (pseudo).
MyTask_1 : Task
MyTask_2 : Task
MyTask_1 t1 = new MyTask_1( ... );
MyTask_2 t2 = new MyTask_2( ... );
TaskExecuter te1 = new TaskExecuter(); // task-thread
TaskExecuter te2 = new TaskExecuter(); // task-thread
te1.AssignTask( t1 );
te2.AssignTask( t2 );
WaitHandle[] taskHandles = {t1.FinishedHandle, t2.FinishedHandle};
WaitHandle.WaitAll( taskHandles );
There is no joining here, just waiting for tasks to finish (the threads
remain alive for performance reasons). The tasks are executed on the
executers which are threads designed to do so. There is no reasonable
timeout because the execution time for the tasks can vary a lot.
I don't see how a Wait + Pulse pattern fits in here. Can one pulse a WaitAll
operation?
Perhaps you have an alternative? And what if the thread doesn't repond. It happens
you know. Then abort is about the best you can do.
[Jon]
Occasionally. A lot of the time I'd rather have a hung thread and abort
the whole process than abort a single thread and have it doing
potentially dangerous stuff.
[TT]
I agree with what you say. Recovery is the best you can do. Reboot the
computer, or reboot the process if this possible (I've never done that). It
is probably ez for a service to do that, I should check that...
There is no avoiding
there the way I see it. Everything has its purpose in System.Threading. They
didn't put it in for the show.
[Jon]
Well, I don't have to necessarily agree that everything should be used.
(Look at the Java API, where Thread.stop() is deprecated as being
basically unsafe.)
Besides, I've seen some evidence that an aborted thread in .NET can
have nasty effects, such as coming out of a Wait without regaining a
lock first. I don't want such a thread running around potentially
causing havoc with the rest of my well-threaded code.
--
Jon Skeet - <
[email protected]>
[TT]
No indeed you don't Jon. But the deprecated stuff in Java is not to be used
of course. And the fact that it was introduced and then deprecated shows
that Java had (serious?) design mistakes from the start. Let's hope that
this not true for .NET.
Now I never done threading in Java (although I've read about it), but you
made me curious as to why Java/Thread::stop was deprecated. Because people
didn't use it correctly, or because it didn't/couldn't do as was documented,
or it did things which are extremely bad for program state/flow.
I agree with you on Thread::Abort: it is not a safe method to use, and there
are nasty side effects (such as jumping out of finally blocks etc).
Cheers,