[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
When programming an application using more than one thread, one has to be very careful about how to synchronize the operations of, and how to arbitrate the interactions between the threads. In particular, it is extremely important that the app's data be kept consistent throughout the execution of the app.
int Var1; int Var1plus2;
Var1 = 5; Var1plus2 = 7;
Var1 = 3; Var1plus2 = 5;
Ok. In this silly example, we have two variables that contain related values. Var1 contains a value, and Var1plus2 always contains Var1 + 2. This code looks OK at first glance, but, when there are multiple preemptive threads working in the system (as in Win95 and above), this gets somewhat hairy. Preemptive measn that the CPU can switch from thread to thread at any time. ANY TIME. That means that if, say, Thread 1 is running and the CPU switches to Thread 2 just before the second line of code is run, then you can potentially end up with the vaules Var1 = 3, Var1plus2 = 3, which is totally incorrect.
The example is very silly and unrealistic, but this same concept (variables that have to be synchronized with the values of other variables) applies to a lot of real-world situations like, for example, inserting a structure into a linked list.
Furthermore, if you use multiple processors to run the different threads (a very good way to optimize an application for speed, and not quite so far-fetched anymore), the processors will run simultaneously for real. In this case, even the usage of a composite type, like a 64-bit integer (__int64 in Visual C++) by itself can result in corrupted data if it's not handled carefully.
Following is a series of useful synchronization objects, and their implementations in the Win32 API. They should always be used carefully, and with full understanding of their implications. All examples are taken from computer game development, but the concepts can be applied to apps of just about any kind. I'll keep adding more as I can:
All trademarked things I mention here are TM by their respective owners. If you are one of those owners and want to be specifically mentioned, please, contact me and I'll include it.
Go back to the main index of JCAB's Rumblings
Wow! hits and increasing...
To contact JCAB: jcab@JCABs-Rumblings.com
Last updated: Wednesday, 07-Apr-2004 01:23:15 PDT [an error occurred while processing this directive]
For a printer-friendly version of this page, click here