Linux, windows version thread mutex

#ifndef CONFIG_ENV_H
#define CONFIG_ENV_H


#define LOCK_NAMESPACE                 LOCK

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

#define BEGIN_LOCK_NAMESPACE   NAMESPACE_BEGIN(LOCK_NAMESPACE)
#define END_LOCK_NAMESPACE             NAMESPACE_END
#define USING_LOCK_NAMESPACE   USING_NAMESPACE(LOCK_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




----------------------------------------------------------------------------------------------------------------------------------------------------


MUTEX_THREAD_H_ #ifndef
#define MUTEX_THREAD_H_

#ifndef _WIN32
#include <pthread.h>
#else
#include <WINDOWS.H>
#endif

#include "config_env.h"

BEGIN_LOCK_NAMESPACE

class API_EXPORT CThreadMutex
{
public:
#ifndef _WIN32
    typedef pthread_mutex_t THREADMUTEX;
#else
    the CRITICAL_SECTION THREADMUTEX typedef;
#endif
    // constructor initializes critical section
    CThreadMutex ();

     // destructor, clears the critical region
     Virtual ~ CThreadMutex ();

     /// enter the critical section
     void the Acquire ();

     /// leaves the critical zone // windows critical section mutex lock time-consuming than saving the time-consuming about five times as long
     void Release ();

! #if (defined (_WIN32_WINNT) && (_WIN32_WINNT> 0x0400)) || defined (_WIN32)
     /// trying to enter the critical region, failed to return a nonzero
      int tryAcquire ();
#endif

Private:
     /// core mutex handle
     m_hMutex THREADMUTEX;
};

class API_EXPORT CAutoMutex
{
public:
        CAutoMutex (CThreadMutex pMutex *)
        {
                m_pMutex = pMutex;
                pMutex-> the Acquire ();
        };
        Virtual ~ CAutoMutex ()
        {
                m_pMutex-> Release ();
        };
Private:
        CThreadMutex * m_pMutex;
};

END_LOCK_NAMESPACE

#endif


-------------------------------------------------------------------------------------------------------------------------------------------


#include "mutex_thread.h"

USING_LOCK_NAMESPACE

CThreadMutex::CThreadMutex()
{
#ifndef _WIN32
    pthread_mutex_init(&m_hMutex, NULL);
#else
        ::InitializeCriticalSection(&m_critclSection);
#endif
}


CThreadMutex::~CThreadMutex()
{
#ifndef _WIN32
    pthread_mutex_destroy(&m_hMutex);
#else
        ::DeleteCriticalSection(&m_critclSection);
#endif
}


void CThreadMutex::Acquire()
{
#ifndef _WIN32
    pthread_mutex_lock(&m_hMutex);
#else
        ::EnterCriticalSection((LPCRITICAL_SECTION)&m_hMutex);
#endif
}


void CThreadMutex::Release()
{
#ifndef _WIN32
    pthread_mutex_unlock(&m_hMutex);
#else
        ::LeaveCriticalSection((LPCRITICAL_SECTION)&m_hMutex);
#endif
}


#ifdef  _WIN32
int CThreadMutex::TryAcquire()
{
        if ( ::TryEnterCriticalSection((LPCRITICAL_SECTION)&m_hMutex) )
        {
                return 0;
        }
        return -1;
}
#endif


---------------------------------------------------------------------------------------

#include "mutex_thread.h"
#include <pthread.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

LOCK::CThreadMutex g_mutex;

int lockcompare(int * lpNum)
{
        //LOCK::CAutoMutex  autolock2(&g_mutex); 
        (*lpNum)++;
        cout<<"e";
        return *lpNum;
}

void * startfunc(void* args)
{
        unsigned int seed = (unsigned int)time(NULL);
        long int data = rand_r(&seed);
        int sh = data%10;
        cout<<(const char*)args<<"  "<<sh<<endl;
        sleep(sh);

        LOCK::CAutoMutex  autolock(&g_mutex);
        //g_mutex.Acquire();
        cout<<(const char*)args<<" locked"<<endl;

        int num = 0;
        time_t t = time(NULL);
        tm tStart;
        localtime_r(&t, &tStart);
        for(int i=0; i<100; i++)
        {
                lockcompare( &num);
        }
        sleep(4);
        t = time(NULL);
        tm tEnd;
        localtime_r(&t, &tEnd);

        cout<<"  compare "<< num <<endl;

        time_t tEn = mktime(&tEnd);
        time_t tSt = mktime(&tStart);
        time_t tTime = tEn - tSt;
        cout<<tEn<<"  "<<tSt<<"use time:"<<tTime<<endl;
        //g_mutex.Release();
        return NULL;
}


int main()
{
    pthread_t  childPid;
    pthread_t  childPid2;
    const char* lpStr1 = "this is the first thread";
    const char* lpStr2 = "this is the second thread";

    pthread_create(&childPid, NULL, startfunc, (void*)lpStr1);
    sleep(1);
    pthread_create(&childPid2, NULL, startfunc, (void*)lpStr2);

    pthread_join(childPid, NULL);
    pthread_join(childPid2, NULL);
    return 0;
}


Guess you like

Origin blog.csdn.net/fengdijiang/article/details/77983728