Chapter 6. Thread SynchronizationThe last chapter showed how processes could create additional threads to carry out background tasks or to wait for some event to occur. However, using threads is not as simple as creating a new thread and leaving it to execute. Since all threads running in an application share the same global resources and variables, there is always the chance that two threads will attempt to access the same resource at the same time. Such simultaneous access of a global resource may cause the program to fail. Because of the way the threads are scheduled, the problems caused by simultaneous access of a global resource will not occur every time the program is run. Typically such synchronization problems occur rarely enough to make tracking them down difficult but frequently enough to be annoying for the user. There is only one sure way to avoid synchronization problems: build inand test thread synchronization techniques whenever you create additional threads. If you have difficulties in writing synchronization code, you are better off staying with a single-threaded application. You can then use other methods, such as timers or sending messages, in place of additional threads.
|