Click here to Skip to main content
15,881,089 members
Articles / General Programming / Threads
Tip/Trick

Locking multiple (up to 2) objects

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
25 Dec 2011CPOL 31.9K   2   1
Lock multiple (up to 2) objects, non blocking
This was needed due to the fact that I needed to obtain locks to multiple objects (stating the obvious) (How to say this in a more interesting way?).

Anyway, if you ever need to lock 2 objects, and are not sure if they are null or not, then try the next code.

This approach will not block and lock both, when 2 non null references are given, or none. It will wait until both are locked, or 1000ms passed.

After 1000ms, default, timeout the DuoEnter method will throw a timeout exception.

C#
internal static void DuoEnter(object po1, object po2, int pnTimeOutMs = 1000)
{
    if ((po1 == null) && (po2 == null))
        return;
    int nMaxLoops = 100 * pnTimeOutMs;
    bool lOneProcessor = Environment.ProcessorCount < 2;
    for (int nLoops = 0; nLoops < nMaxLoops; nLoops++)
    {
        if ((po1 == null) || (po2 == null) || (po1 == po2))
        {
            if (Monitor.TryEnter(po1 ?? po2))
                return;
        }
        else
        {
            if (Monitor.TryEnter(po1))
                if (Monitor.TryEnter(po2))
                    return;
                else
                    Monitor.Exit(po1);
        }
        if (lOneProcessor || (nLoops % 100) == 99)
            Thread.Sleep(1); // Never use Thread.Sleep(0)
        else
            Thread.SpinWait(20);
    }
    throw new TimeoutException(
        "Waited more than 1000 mS trying to obtain locks on po1 and po2");
}

internal static void DuoExit(object po1, object po2)
{
    if ((po1 == null) && (po2 == null))
        return;
    if (po1 == null || po2 == null || po1 == po2)
        Monitor.Exit(po2 ?? po1);
    else
    {
        Monitor.Exit(po2);
        Monitor.Exit(po1);
    }
}


Use it as in:

C#
internal void InternalDispose()
{
    List<TreeCachedNodeInfo> aoTreeCachedNodeInfoList
        = this._aoTreeCachedNodeInfoList;
    List<Node> aoNodeList
        = this._aoNodeList;
    DuoEnter(aoNodeList, aoTreeCachedNodeInfoList);
    try
    {
        if (aoNodeList != this._aoNodeList)
            throw new InvalidOperationException(
                "aoNodeList != this._aoNodeList");
        if (aoTreeCachedNodeInfoList != this._aoTreeCachedNodeInfoList)
            throw new InvalidOperationException(
                "aoTreeCachedNodeInfoList != this._aoTreeCachedNodeInfoList");
        if (aoNodeList != null)
        {
            this._aoNodeList = null;
            aoNodeList.Clear();
        }
        if (aoTreeCachedNodeInfoList != null)
        {
            this._aoTreeCachedNodeInfoList = null;
            aoTreeCachedNodeInfoList.Clear();
        }
    }
    finally
    {
        DuoExit(aoNodeList, aoTreeCachedNodeInfoList);
    }
}

That's all, I hope you like it.

Regards,

Frans de Wit

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
FDW
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 This code smells. Combining a monito... Pin
YvesDaoust26-Dec-11 21:32
YvesDaoust26-Dec-11 21:32 

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

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