Fabulous Adventures In Coding

Eric Lippert's Blog

mutex.cpp

#include "headers.h"

Mutex::Mutex()
{
    m_fInitialized = FALSE;
}

HRESULT Mutex::Create(Mutex * * ppMutex)
{
    AssertOutPtr(ppMutex);

    HRESULT hr;
    BOOL fSuccess;
    DWORD error;
    Mutex * pMutex = NULL;

    pMutex = new Mutex();
    if (NULL == pMutex)
    {
        hr = E_OUTOFMEMORY;
        goto LError;
    }

    fSuccess = InitializeCriticalSectionAndSpinCount(&pMutex->m_criticalsection, 0);
    if (!fSuccess)
    {
        error = GetLastError();
        hr = HRESULT_FROM_WIN32(error);
        goto LError;
    }

    pMutex->m_fInitialized = true;
    *ppMutex = pMutex;
    pMutex = NULL;
    hr = S_OK;

LError:

    if (NULL != pMutex)
        delete pMutex;

    return hr;
}

Mutex::~Mutex()
{
    if (this->m_fInitialized)
        DeleteCriticalSection(&this->m_criticalsection);
}

void Mutex::Enter(void)
{
    EnterCriticalSection(&this->m_criticalsection);
}

void Mutex::Leave(void)
{
    LeaveCriticalSection(&this->m_criticalsection);
}

Published Monday, April 19, 2004 6:22 PM by Eric Lippert
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker