> 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. :-)
--