User`s guide

5 Acquiring Image Data
5-30
Using Critical Sections
This section describes how to use critical sections to protect portions of your adaptor code.
The section describes the adaptor kit's main critical section class, ICriticalSection,
and the ancillary class, IAutoCriticalSection, that you use to manage critical
sections. Topics covered include
“Understanding Critical Sections” on page 5-30
“Example: Using a Critical Section” on page 5-31
Understanding Critical Sections
To prevent sections of code or resources from being accessed simultaneously by multiple
threads, use critical section (ICriticalSecton) objects. The basic process for using a
critical section has three-steps:
1
Create a critical section object, using the adaptor kit createCriticalSection()
function.
2
At the point in your code that you want to protect, enter the critical section by calling
the ICriticalSection::enter() member function.
3
At the end of the code that you want to protect, leave the critical section by calling
the ICriticalSection::leave() member function.
While this process might appear simple, using a ICriticalSection object directly
in this way can expose your adaptor to problems. For example, if an error occurs in the
protected code, the call to the leave() function might never be executed. Entering a
critical section and then never leaving it can cause unexpected results.
To make working with critical sections easier, the adaptor kit provides a second class,
called IAutoCriticalSection, that can help you manage the critical sections you
define.
You first create an ICriticalSection object and then pass this object
to the createAutoCriticalSection() function when you create the
IAutoCriticalSection object. When you create the object, you automatically enter
the critical section without having to call the enter() function. When the protected
code goes out of scope, the auto critical section automatically leaves the critical section
without your code having to call the leave() function.