If anyone is interested the following code has been used safely on Windows
and Linux for thread safe reference counting.  You can paste it into a
header file if you want, or use it as a guide.  It uses pthreads on Linux
and the Win32 thread functions on Windows with InterlockedIncrement() and
InterlockedDecrement().

Best regards,
Martin


#ifndef Increment_h
#define Increment_h

#define _WINDOWS_

#ifndef _WINDOWS_
#include <pthread.h>
#include <bits/pthreadtypes.h>
#endif

class CIncrement
{
public: 

        #ifdef _WINDOWS_
        CRITICAL_SECTION m_CS;
        #endif

CIncrement()
{
        #ifndef _WINDOWS_
        pthread_mutex_init(&m_Mutex, NULL);
        #endif
    
        #ifdef _WINDOWS_
        InitializeCriticalSection(&m_CS);
        #endif

        #ifdef _WINDOWS_
        EnterCriticalSection(&m_CS);
        #endif

        m_nVal = 0;

        #ifdef _WINDOWS_
        LeaveCriticalSection(&m_CS);
        #endif
}

~CIncrement()
{
        #ifndef _WINDOWS_
        pthread_mutex_destroy (&m_Mutex);
        #endif

        #ifdef _WINDOWS_
        DeleteCriticalSection(&m_CS);
        #endif
}

CIncrement& operator=(const CIncrement& thread)
{
    if (this == &thread)
        return *this;

        #ifndef _WINDOWS_
    pthread_mutex_lock(&m_Mutex);
    #endif

        #ifdef _WINDOWS_
        EnterCriticalSection(&m_CS);
        #endif

    m_nVal = thread.m_nVal;

        #ifdef _WINDOWS_
        DeleteCriticalSection(&m_CS);
        #endif
    
    #ifndef _WINDOWS_
    pthread_mutex_unlock(&m_Mutex);
    #endif
    
    return *this;
}   

int operator++(int nVal)
{
    return Add(1, true);
}

int operator++()
{
    return Add(1, false);
}
    
int operator--(int nVal)
{
    return Add(-1, true);
}

int operator--()
{
    return Add(-1, false);
}
    
bool operator==(const int nVal)
{
    #ifndef _WINDOWS_
    pthread_mutex_lock(&m_Mutex);
    #endif

        #ifdef _WINDOWS_
        EnterCriticalSection(&m_CS);
        #endif
    
    bool bReturn = (m_nVal == nVal);

        #ifdef _WINDOWS_
        LeaveCriticalSection(&m_CS);
        #endif

    #ifndef _WINDOWS_
    pthread_mutex_unlock(&m_Mutex);
    #endif
    
    return bReturn;
}

int Add(int nNum, bool bReturnNew)
{
    int rtn = 0;
    #ifdef _WINDOWS_
    return nNum > 0 ? InterlockedIncrement((volatile LONG*) &m_nVal)
                                        : InterlockedDecrement((volatile
LONG*) &m_nVal);

    #else
    pthread_mutex_lock(&m_Mutex);
    int oVal = m_nVal;
    m_nVal += nNum;
    rtn = bReturnNew ? m_nVal : oVal;
    pthread_mutex_unlock(&m_Mutex);
    #endif
    
    return rtn;
}

protected:
        int m_nVal;

        #ifdef _WINDOWS_
        void* m_Mutex;
        #else
        pthread_mutex_t m_Mutex;
        #endif
};

#endif











-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Tamas Szekeres
Sent: Saturday, November 01, 2008 5:40 PM
To: [email protected]
Subject: [gdal-dev] GDAL reference counting is thread unsafe

Folks,

I've found the following issue with the current refcounting
implementation in GDAL:
http://trac.osgeo.org/gdal/ticket/2648

Do you have idea whether we could handle this problem possibly for the
next release?


Best regards,

Tamas
_______________________________________________
gdal-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/gdal-dev


_______________________________________________
gdal-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to