Access synchronization
When synchronizing access, as we saw in the previous demo, one of the main issues with multithreaded applications is the need to avoid multiple threads accessing the same variable or the same resource at the same time. If one thread is making a change to a data structure while another thread is reading from it, the result will be totally unpredictable. Synchronized access to resources is the most complex issue of threading.
There are a large number of techniques you can use for thread synchronization and we don’t have the scope to explore them all extensively in this chapter. I will just introduce the most common and simple options here.
Calling Synchronize
The first option is the one we have already used: the Synchronize
method of the TThread
class. The idea is simple: there is some code that needs to be executed in the context of the main thread. This is the case for UI-related code, as mentioned earlier. In general, if you need to make sure...