Critical Sections

A critical section identifies code that must be executed to completion before another piece of code can be executed. In the example presented in the previous section, the statements "g_fValue = g_fValue * g_fValue;" and "g_fValue = 3.0 + g_fValue;" should be marked as critical sections to ensure that both statements can be executed to completion before the other starts executing. If this is done, the only two possible results in g_fValue are 103 and 169. The spurious value of 130 will never occur.

To create and use a critical section you should:

  • Declare a CRITICAL_SECTION structure as a global variable, or a member variable of a class

  • Call the InitializeCriticalSection function to initialize this structure

  • Call EnterCriticalSection ...

Get Windows® CE 3.0 Application Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.