5,666,547 members and growing! (14,405 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Threads     Intermediate

Thread Synchronization Classes

By Zoran M. Todorovic

Implements a set of classes for thread synchronization.
VC6, C++Windows, NT4, Visual Studio, MFC, Dev

Posted: 29 May 2000
Updated: 29 May 2000
Views: 48,315
Bookmarked: 8 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 4.45 Rating: 3.27 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
1 vote, 33.3%
5
  • Download source files - 2 Kb
  • This article describes a set of classes that are used to perform synchronization between different threads. The class diagram below should explain the idea:

    At the root, there is a TSynchroBase class that has 2 abstract virtual methods: (1) DoLock() and (2) DoUnlock(). These 2 methods are implemented in derived classes because they contain selected synchronization method specific code.

    Two concrete classes are supplied:

    1. TSynchroMutex that uses a mutex for synchronization.
    2. TSynchroCriticalSection that uses a critical section for synchronization.

    However, additional classes may be implemented (for example, a class that uses events or semaphores).

    When synchronization is used in the application, one has to take care that the synchronization object is unlocked once the thread that took ownership of the synchronization object is finished with it. If this is not done, other threads will be blocked. This can be difficult and error prone in a complex function where there is more then one exit point from a function. A simple solution is to move the protected code to a function and this function is called after taking ownership of the synchronization object. After the function, the synchronization object is released. This is illustrated in the following sample code.

    Error prone example:

    ...
    Lock()
    ... 
    // Code block with multiple exit points
    
    if (...)
    { 
       Unlock(); 
       return; 
    } 
    ...
    Unlock();
    

    Better solution:

    ...
    Lock();
    Function();	// Function with multiple exit points
    
    Unlock();
    ...
    

    In the first example it is easy to forget a single Unlock() which is a logical error that may block other threads since they cannot take ownership of the synchronization object.

    The second example clutters the code with unnecessary functions.

    TSynchroTransient class is designed to solve this problem. It takes a pointer to a synchronization object in a constructor and immediately takes ownership of the synchronization object. It will automatically release the synchronization object in its destructor.

    TSynchroMutex g_Synchro;
    
    function()
    {
        TSynchroTransient synchro(&g_Synchro);
    	if (synchro.IsLocked())
    	{
    	    // Do whatever
    
    	}
    }
    

    For this to function properly, it is necessary that the TSynchroTransient object is created on the stack -- it will be automatically destroyed when the function exits. The IsLocked() check is needed only if you use a synchronization method that may fail (for example, mutex). If you use critical sections, it is not necessary to call this function since entering critical section blocks until is succeeds.


    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Zoran M. Todorovic


    I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.
    Occupation: Web Developer
    Company: SCA d.o.o.
    Location: Serbia Serbia

    Other popular Threads, Processes & IPC articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
    Generalthe same code in MFCsussYury Goltsman21:37 29 May '00  
    GeneralRe: the same code in MFCsussZoran M.Todorovic3:41 30 May '00  
    GeneralRe: the same code in MFCsussYury Goltsman6:27 30 May '00  
    GeneralRe: the same code in MFCsussRandy Pitz4:59 27 Jul '00  
    GeneralRe: the same code in MFCsussOleg Cherkasenko22:29 25 Aug '02  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 29 May 2000
    Editor: Valerie Bradley
    Copyright 2000 by Zoran M. Todorovic
    Everything else Copyright © CodeProject, 1999-2008
    Web17 | Advertise on the Code Project