An object of class CMutex
represents a ?mutex? ? a synchronization object that allows one thread
mutually exclusive access to a resource. Mutexes are useful when only
one thread at a time can be allowed to modify data or some other
controlled resource. For example, adding nodes to a linked list is a
process that should only be allowed by one thread at a time. By using a
CMutex object to control the linked list, only one thread at a time can
gain access to the list.
To use a CMutex object, construct the
CMutex object when it is needed. Specify the name of the mutex you wish
to wait on, and that your application should initially own it. You can
then access the mutex when the constructor returns. Call
CSyncObject::Unlock when you are done accessing the controlled resource.
An
alternative method for using CMutex objects is to add a variable of
type CMutex as a data member to the class you wish to control. During
construction of the controlled object, call the constructor of the
CMutex data member specifying if the mutex is initially owned, the name
of the mutex (if it will be used across process boundaries), and desired
security attributes.
To access resources controlled by CMutex
objects in this manner, first create a variable of either type
CSingleLock or type CMultiLock in your resource?s access member
function. Then call the lock object?s Lock member function (for example,
CSingleLock::Lock). At this point, your thread will either gain access
to the resource, wait for the resource to be released and gain access,
or wait for the resource to be released and time out, failing to gain
access to the resource. In any case, your resource has been accessed in a
thread-safe manner. To release the resource, use the lock object?s
Unlock member function (for example, CSingleLock::Unlock), or allow the
lock object to fall out of scope
The main difference between ASSERT and VERIFY is when you compile the release build of the program.
ASSERT will not be present in the release build version of the executables/dlls, and its expression that would have been evaluated will be deleted.
VERIFY will be present in the release build version of the executables/dlls but its expression that would have been evaluated will be left intact.
Threads
are similar to processes, but differ in the way that they share
resources. Threads are distinguished from processes in that processes
are typically independent, carry considerable state information and have
separate address spaces. Threads typically share the memory belonging to their parent process.
VC++ FAQ?s And Interview Questions and Answers
No comments :
Post a Comment