Threads so darn hard to understand

A

Arafangion

Hello, recently I've been trying to figure out how the heck to just
destroy a thread.

I have since rewritten the code in question, but what I was trying to do
was to use the .Abort() method, but that would fail if the thread was in
a Suspended case.

Now, my book doesn't even hint (Deitel Developer Series, C# for
Experienced Programmers) that a thread might exist in more than one
state, which was also part of my problem. (Fortunately during a period
of high inspiration I managed to figure out that ThreadState is a
bitfield and OR'ing the threadstates work).

However, it appears to be impossible to do the following:
For a given thread, stop it.

I have tried exhaustively checking the state of the thread and doing the
appropriate .Resume(), .Interrupt(), etc, however by the time I do
..Abort(), I cannot guarantee that the statement will execute successfully.

Currently I've implemented my very own .AbortRedrawing() method, but
I've wasted hours trying to understand threads themselves. I do not
understand why there's no .ForceAbort() method.

If anyone could show me some documentation (more useful than the "wow,
here's how to create three threads!" kind of tutorial), that'd be great.

(Sorry if the tone is a bit... But I've been wrestling with it for a
while).

Thanksyou.

(PS: I have since discovered the Timer() class, which would've fit my
purpose better, but the fact stands that I still need to learn more
about threading in .NET)
 
J

Jon Skeet [C# MVP]

Arafangion said:
Hello, recently I've been trying to figure out how the heck to just
destroy a thread.

Using Abort (or even Interrupt) is usually a bad way of doing it.
Currently I've implemented my very own .AbortRedrawing() method, but
I've wasted hours trying to understand threads themselves. I do not
understand why there's no .ForceAbort() method.

Because aborting threads almost always inherently leads to instability.
If anyone could show me some documentation (more useful than the "wow,
here's how to create three threads!" kind of tutorial), that'd be great.

See http://www.pobox.com/~skeet/csharp/threads/

I hope it fits your needs.
 
O

Olaf Baeyens

Hello, recently I've been trying to figure out how the heck to just
destroy a thread.
Threading is not that easy to do, unless you have a lot of programming
experience.
The problem with threads is not that the code is difficult, but when threads
interact with each other you could get problems that is very hard to
visualize.

Some things I have learend about threads.
* Mark your functions/propertes when they are thread safe in startup mode
and in run mode.
I use this notation {RWrw} RW means it is safe to Write/Read even during
setting up the thread.
The rw is that it is safe to read/write during running.

If I write this {--rw} then I should pause the thread if I initialize/call
these this property/function.
Basically creating a thread in syspended mode, the fill in these variables
and then start the thread.

If I write this {----} Then this propertie function is totally none-thread
safe and must not be called while the thread is running from outside the
thread. Normally these functions/property will be marked private since it is
perfectly safe inside the thread to use it's own variables functions. The
problem only occurs when the functions/properties arre called from another
thread.

If you create threads try to avoid as much communication between 2 threads
and if you do it, then do it in a atomic operation. Try to create a
structure that contains all the parameters you want to access, then lock the
thread, copy the internal properties to that structure and the unlock. This
way you have all parameters synchronized with each other and reduces the
time waiting for e loack release.

Stopping a thread is another problem. You should send a command to request a
thread to stop and then you wait. Aborting a thread is not a good way to do.
So your code must be good. But if you have to stop multiple threads, then
the best way to do is to loop through all threads to stop itself, and then
wait untill all of them signaled that they are stopped. This is the fastest
way to stopp multiple threads. But you might get an out of order
synchronization problem. Suppose that you have 10 threads, you loop from
1..10 to send the stop command, then the stop process could be out of
sequence e.g. 5 stops, then 2 then 9 then 3 then 4 then 1 then 7...

Don't feel bad if you have a hard time to learn it. It took me also a hell
of a time to get it right.

Ah one more thing, keep those critical sections as low as possible, because
if you try to access a locked property then you code stalls because it must
wait untill it gets released. If this stalled thread happens to also lock a
critical section then it migth stall another thread on its turn. You will
see that the stall gives a big performance penalty that is not lineair if
you gave more threads. It is more quadratic in nature. I mean, If you use 10
threads, then you migth lose 100 ms, but if you use 20 threads you might
stall for 400 ms... So only lock when it is abselutely necessary and try to
lock only the things that cannot go wrong and is very fast.

For example, if you load a file in a thread, use a second buffer, load the
file (unlocked) in the temporary buffer and then copy that buffer to your
second thread locked. This way if you have a network hickup, then code does
not freeze. Like access denied.

Sadly enough there is no one fit all solution when you work with threads. It
must be custom made.

Good luck. :)

--
 
A

Arafangion

Olaf said:
Sadly enough there is no one fit all solution when you work with threads. It
must be custom made.

Ok, I think I understand what one has to do now, although the actual
code itself will take practise, but I do wonder why there isn't at least
a generic .Interrupt() method that is _guaranteed_ to raise the relevant
exception in the thread.
 
H

Helge Jensen

Arafangion said:
but I do wonder why there isn't at least
a generic .Interrupt() method that is _guaranteed_ to raise the relevant
exception in the thread.

For one thing, the thread can be blocked outside the control of the CLR,
for example in some machine-code.

Even worse, it can be blocked inside the OS in a non-interruptable wait.

Valid async-interruptable machine- (and c-,...) -code is *insanely* hard
to write.
 
O

Olaf Baeyens

Sadly enough there is no one fit all solution when you work with
threads. It
Ok, I think I understand what one has to do now, although the actual
code itself will take practise, but I do wonder why there isn't at least
a generic .Interrupt() method that is _guaranteed_ to raise the relevant
exception in the thread.
I don't know what you mean with generic Interrupt()

But I do know that VB and COM? have a multithread model called appartment
threading that automatically locks the properties and methods when called
(if I understand this correctly). So for beginners this might help a little
but it is not the most efficient way of doing things.

--
 
A

Alvin Bruney [MVP - ASP.NET]

VB has apartment threading? I wasn't aware of that.

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc, Amazon.com etc
 
O

Olaf Baeyens

VB has apartment threading? I wasn't aware of that.I am not a VB programmer, I only see it for minimum 1 meter distance. :)
Afraid to come near. :)

But if I Google, then I see many references to VB and apartment threading
(STA/MTA) I believe it has been available since VB 5 with SPxx and VB 6.
 
A

Alvin Bruney [MVP - ASP.NET]

i'll have to do some looking around. bahhhh never mind. i'm not that
interested in it anyway

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc, Amazon.com etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top