Question

I have these classes in my lib.

class SingleLock
{
public:
    SingleLock(CRITICAL_SECTION *pCS);
    ~SingleLock();

private:
    LPCRITICAL_SECTION m_cs;
};

SingleLock::SingleLock(CRITICAL_SECTION *pCS)
{
    m_cs = pCS;
    EnterCriticalSection(m_cs);
}
SingleLock::~SingleLock()
{
    LeaveCriticalSection(m_cs);
}

template <typename T>
class Callback
{
public:
    Callback() : m_current(), m_total(), m_cs()
    {
        InitializeCriticalSection(&m_cs);
    }

~Callback()
{
    DeleteCriticalSection(&m_cs);
}

void SetCurrent(T current)
{
    SingleLock(&m_cs);
    m_current = current;
}

void SetTotal(T total)
{
    SingleLock(&m_cs);
    m_total = total;
}

T GetCurrent()
{
    SingleLock(&m_cs);
    return m_current;
}

T GetTotal()
{
    SingleLock(&m_cs);
    return m_total;
}

private:
    T m_total;
    T m_current;
    CRITICAL_SECTION m_cs;
};

The Callback class compiles but while try to use some method in code, my code doesn't compile. I have changed it into this:

template <typename T>
class Callback
{
public:
    Callback() : m_current(0), m_total(0), m_cs()
    {
        InitializeCriticalSection(&m_cs);
    }

    ~Callback()
    {
        DeleteCriticalSection(&m_cs);
    }

    void SetCurrent(T current)
    {
        EnterCriticalSection(&m_cs);
        m_current = current;
        LeaveCriticalSection(&m_cs);
    }

    void SetTotal(T total)
    {
        EnterCriticalSection(&m_cs);
        m_total = total;
        LeaveCriticalSection(&m_cs);
    }

    T GetCurrent()
    {
        EnterCriticalSection(&m_cs);
        T result = m_current;
        LeaveCriticalSection(&m_cs);
        return result;
    }

    T GetTotal()
    {
        EnterCriticalSection(&m_cs);
        T result = m_total;
        LeaveCriticalSection(&m_cs);
        return result;
    }

private:
    T m_total;
    T m_current;
    CRITICAL_SECTION m_cs;
};

And now everything works file, can somebody explain why this happens?

Was it helpful?

Solution

Not happy about it, but after posting question immediately found mistake, I need SingleLock sl(&m_cs); instead of SingleLock(&m_cs);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top