Threads operate with the same variables as the main task.
Subsequently, while both are running *simultaneously* (watch how you take that stmt without multiple cpu(s)) you can "use" variables in both/multiple threads and thus *screw* yourself.
Thread-safe means you have taken measures to *not* fall into that trap.
As for myself, I generally run a thread kind of like a program. I define some input "parm" variables, define some for just the thread (those that can't be put on the stack), and a place for the "response" (more variables).
I don't know if it's *eloquent* or not, but it works.
Non-thread-safe code may consist of two threads (one of which may be the main program thread), and where they *can* both update a variable (ie. loop := loop + 1). The variable would obviously have to be visible to both, and for simplicity lets assume in a global variable declared in a common unit.
The issue arises when the both may be attempting to modify the variable at the same time which can cause no end of problems. Delphi has a few mechanisms to prevent this. The help file has a lot of good info on learning how to use threads properly. Also, consult
for heaps of information on the topic. That's chapter 8 of an excellent website (contents page link at the bottom). I recommend slogging through it all.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.