Click here to Skip to main content
Licence 
First Posted 21 May 2003
Views 41,558
Bookmarked 7 times

Using templated mutex variables

By | 21 May 2003 | Article
Templated class to transparently use variables associated with mutexes

Sample Image - MutexVars.jpg

Introduction

This article demonstrates the use of mutexes and templates to achieve transparency in source code when dealing with mutexed variables. It's also small hack to minimize code rewriting when you want multi-threaded solutions ported from single-threaded ones.

Next, i'll give you some background about the concepts behind this article and how to use the developed code in your applications.

Background

Let's start with a simple example of a single-threaded application performing some operations:

boolean CanStartStep2 = false;

void compute()
{
    for (int i = 0; i < 500; i++)
    {
        // do some work
        if (i == 50)
            CanStartStep2 = true;
    }

    while (!CanStartStep2)
        ;

    for (int i = 0; i < 100; i++)
    {
        // do some work
    }
}
It's easy to understand that the given example could be split into worker threads, something like the next example (keep in mind that this is a simple example):
boolean CanStartStep2 = false;
CMutex mut;

static UINT Thread1Proc(LPVOID)
{
    mut.Lock();
    CanStartStep2 = false;
    mut.Unlock();

    for (int i = 0; i < 50000; i++)
    {
        // do some work
        if (i == 10)
        {
            mut.Lock();
            CanStartStep2 = true;
            mut.Unlock();
        }
    }

    return 0;
}

static UINT Thread2Proc(LPVOID)
{
    boolean b;

    do {
        mut.Lock();
        b = CanStartStep2;
        mut.Unlock();
    } while (!b);

    for (int i = 0; i < 100; i++)
    {
        // do some work
    }

    return 0;
}
You can see that when you need to access the shared variable you always have to work explicitly with a mutex. This article explains a simple, yet powerfull way to use variables without explicit uses of mutexes. It's pretty useful when dealing with any type of global application state variables, etc..

To achieve this functionallity, i've implemented a small templated class MutexVar<typename T> wrapping up these concepts.
The previous example can be rewritten, cleaning up all the explicit mutex Lock() and Unlock() calls:

MutexVar<boolean> CanStartStep2 = false;

static UINT Thread1Proc(LPVOID)
{
    for (int i = 0; i < 50000; i++)
    {
        // do some work
        if (i == 10)
            CanStartStep2 = true;
    }

    return 0;
}

static UINT Thread2Proc(LPVOID)
{
    while (!CanStartStep2)
        ;

    for (int i = 0; i < 100; i++)
    {
        // do some work
    }

    return 0;
}

Using the code

It's easy to use the code, just follow these steps:

Include the header file:

#include "MutexVar.h"

Declare a variable (e.g. a mutexed int variable):

MutexVar<int> i = 0;

Start using it like any other int, e.g.:

i = 100;
int j = i * 2;

cout << i << endl;

Conclusions

I hope this article can help you develop shorter, more efficient and more reusable code. That's all, folks!

Bugs

  • Some operators don't work (++, --, amongst others);
  • All functions using va_args (printf, etc.) need an explicit cast to read a variable value, or else will use variable memory address (thanks walker_andrew_b);
  • I'm pretty sure there are some more bugs i haven't found...

History

Version 0.1.0: 17/05/2003

  • Initial release

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

Rui Dias Lopes

Web Developer

Portugal Portugal

Member

Rui Lopes is doing a Post-Graduation on Digital Talking Books and does some research on Human Computer Interaction and Multimedia at the Faculty of Sciences, University of Lisbon, Portugal.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalproblem Pinmembergaz6622:31 23 Sep '04  
GeneralSleep PinmemberPaul Evans23:58 26 May '03  
GeneralRe: Sleep PinmemberRui Dias Lopes16:37 27 May '03  
GeneralRe: Sleep PinmemberPaul Evans22:18 27 May '03  
GeneralRe: Sleep PinmemberRui Dias Lopes5:50 28 May '03  
GeneralWell done PinmemberPaul Evans22:45 26 May '03  
GeneralRe: Well done PinmemberRui Dias Lopes16:28 27 May '03  
GeneralRe: Well done Pinmemberpeterchen5:54 20 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 May 2003
Article Copyright 2003 by Rui Dias Lopes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid