linux读写锁

#ifndef CONFIG_ENV_H
#define CONFIG_ENV_H


#define RWLOCK_NAMESPACE                 RWLOCK

#define NAMESPACE_BEGIN(x)              namespace x {
#define NAMESPACE_END                   }
#define USING_NAMESPACE(x)              using namespace x;


#define BEGIN_RWLOCK_NAMESPACE   NAMESPACE_BEGIN(RWLOCK_NAMESPACE)
#define END_RWLOCK_NAMESPACE             NAMESPACE_END
#define USING_RWLOCK_NAMESPACE   USING_NAMESPACE(RWLOCK_NAMESPACE)


#if defined(_WIN32) && !defined(SEPARATE_COMPILE)
        #ifdef DLL_API_EXPORT
                #define API_EXPORT __declspec(dllexport)
        #else
                #define API_EXPORT __declspec(dllimport)
        #endif
#else
        #define API_EXPORT
#endif

#endif


#ifndef PTHREAD_RW_LOCK_H_
#define PTHREAD_RW_LOCK_H_


#include "config_env.h"
#include <pthread.h>

BEGIN_RWLOCK_NAMESPACE
class API_EXPORT CReadWriteLock
{
public:
        CReadWriteLock();
        ~CReadWriteLock();


        int ReadAcquire();


        int ReadRelease();


        int WriteAcquire();


        int WriteRelease();
private:
#ifdef _WIN32

#else                   //for posix unix
        pthread_rwlock_t        m_rwl;
#endif


        CReadWriteLock(CReadWriteLock &lock);
        CReadWriteLock & operator=(CReadWriteLock &lock);
};
END_RWLOCK_NAMESPACE

#endif


//---------------------------------------------------------------------------
#include "pthread_rw_lock.h"


USING_RWLOCK_NAMESPACE


CReadWriteLock::CReadWriteLock()
{
#ifdef _WIN32


#else
        pthread_rwlock_init(&m_rwl, NULL);
#endif
}


CReadWriteLock::~CReadWriteLock()
{
#ifdef _WIN32


#else
        pthread_rwlock_destroy(&m_rwl);
#endif
}




CReadWriteLock::CReadWriteLock(CReadWriteLock &lock)
{
}


CReadWriteLock & CReadWriteLock::operator=(CReadWriteLock &lock)
{
}


int CReadWriteLock::ReadAcquire()
{
        int nRet = -1;
#ifdef _WIN32


#else
        nRet = pthread_rwlock_rdlock(&m_rwl);
#endif
        return nRet;
}


int CReadWriteLock::ReadRelease()
{
        int nRet = -1;
#ifdef _WIN32


#else
        nRet = pthread_rwlock_unlock(&m_rwl) ;
#endif
        return nRet;

}


int CReadWriteLock::WriteAcquire()
{
        int nRet = -1;
#ifdef _WIN32


#else
        nRet = pthread_rwlock_wrlock(&m_rwl) ;
#endif
        return nRet;
}


int CReadWriteLock::WriteRelease()
{
        int nRet = -1;
#ifdef _WIN32


#else
        nRet = pthread_rwlock_unlock(&m_rwl) ;
#endif
        return nRet;
}



猜你喜欢

转载自blog.csdn.net/fengdijiang/article/details/77989164