User`s guide
Using Critical Sections
5-31
The auto critical section object ensures that you always exit a critical section. However,
you must also ensure that the auto critical section itself gets deleted. To do this,
the adaptor kit recommends managing the handle to the IAutoCriticalSection
object, returned by createAutoCriticalSection(), as an auto_ptr using the
std::auto_ptr<> template class from the Standard Template Library. The auto_ptr
helps ensure that the IAutoCriticalSection handle is deleted.
Example: Using a Critical Section
To define a section of code as a critical section, follow this procedure.
1
Create an ICriticalSection object, using the createCriticalSection()
function. Adaptors typically create an ICriticalSection object in their
constructors — see “Implementing Your Adaptor Class Constructor” on page 4-12.
_mySection = imaqkit::createCriticalSection();
The function returns a handle to an ICriticalSection object. _mySection, which
is declared as a member variable in the adaptor class header file, as follows.
imaqkit::ICriticalSection* _mySection;
2
At the point in your code that you want to protect, create an
IAutoCriticalSection object. The IAutoCriticalSection class guarantees
that the critical section objects are released when the protected code goes out of
scope, or if an exception occurs. In an adaptor, you typically want to protect the
frame acquisition loop in a critical section. Insert this code in the acquisition thread
function, just before the frame acquisition loop — see “Implementing the Acquisition
Thread Function” on page 5-18.
std::auto_ptr<imaqkit::IAutoCriticalSection>
myAutoSection(imaqkit::createAutoCriticalSection(adaptor->_mySection,
true));
In this code, the variable myAutoSection is a handle to an
IAutoCriticalSection object, that is managed as a Standard Template
Library auto_ptr. The code passes a handle to an ICriticalSection object,
_mySection, as an argument to the createAutoCriticalSection() function.
The second argument to createAutoCriticalSection() specifies that the
adaptor should enter the critical section automatically upon creation of the
IAutoCriticalSection.
3
At the end of the code that you want to protect, leave the critical section. In an
adaptor, you want to leave the critical section after the frame acquisition loop is