Easy Thread Safe Resource Synchronization in MFC

Environment: VC6 SP5 Win9x, WinNT 4.0, Win2000

When a program is using more then one thread, there will be in most cases the need of synchronizing the data. This can be 'easily' done by using one of the provided synchronization classes of MFC: CMutex, CSemaphore, CEvent, CCriticalSection. In some of the simple cases these classes are everything one needs to implement the desired syncronisation between all treads.

To obtain access to a locked resource you have to use a CSingleLock or CMultiLock object. These objects have a simple interface. Either you are granted access to the data or you don't. But sometimes we need a more complex locking mechanism and then we are left in the blue. I needed a mechanisme that could be called something like this : 'Single-Write, Multiple-Read, Write-Has-Priority-To-Read'.

Single-Write means that only one thread is granted access in order to edit the protected data. When a request to write or read the data comes, the requesting thread is blocked.

Multiple-read means that multiple threads are granted access to read the data at the same time. A request to edit the data will blocked, requests to read will be granted access.

>Write-Has-Priority-To-Read means that when a request to edit the data is done, while the data is locked, the calling thread waits until the reading thread(s) are finnished. When there are new requests to read the data after the 'write-request' is done the threads are blocked until the writing-operation is finneshed, eventhough the write-thread is still waiting until he is granted access to the data.

To create this behaviour I create my own locking object. This encloses a CMutex and two CEvents. The CMutex is used to control the internal data. The two events are used to signal a write-thread or some read-thread that the access to the data is granted. This object is has alsoo a simple interface to control the data:

READING:

  • void StartReading();
  • void StopReading();

Sample code:

... lock.StartReading (); 

// we have access now 
... lock.StopReading (); 

WRITING:

  • BOOL StartWriting();
  • void StopWriting();

When two threads launch a request for edit the data only one thread may be granted access. The request of the other thread is denied. The 'StartWriting' function returns whether the request is accepted or not. Usign a loop it is possible to allow multiple threads requesting a write-operation.

Sample code:

while (!lock.StartWriting) Sleep (100); // wait a little while
// we have access now ...
lock.StopWriting ();

Downloads

Download source - 2 Kb


Comments

  • Easy Thread Safe Resource Synchronization in MFC

    Posted by Legacy on 11/15/2002 12:00am

    Originally posted by: Joe

    The synchronization has a dead lock. So I would not recommend that you use it.


    Reply
  • Easy Thread Safe Resource Synchronization in MFC

    Posted by Legacy on 04/29/2002 12:00am

    Originally posted by: satya

    It's very nice article good example.But how can we use CMultiLock in the place of CSingleLock class, is there any advantage of using CMultiLock in the place of CSingleLock.
    Please provide example using CMultiLock class.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds