Linux、windows版本线程互斥锁

#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

 

 

 

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

 

#ifndef MUTEX_THREAD_H_
#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
    typedef CRITICAL_SECTION    THREADMUTEX;
#endif
    //构造函数,初始化临界区
    CThreadMutex();

     //析构函数, 清除临界区
     virtual ~CThreadMutex();

     ///进入临界区
     void Acquire();

     ///离开临界区//windows 临界区锁耗时比互斥锁耗时节约了大概5倍的时间
     void Release();

#if (defined (_WIN32_WINNT) && (_WIN32_WINNT > 0x0400)) || !defined(_WIN32)
     ///尝试进入临界区,失败返回非零
      int TryAcquire();
#endif

private:
     ///核心互斥量句柄
     THREADMUTEX  m_hMutex;
};

class API_EXPORT CAutoMutex
{
public:
        CAutoMutex(CThreadMutex * pMutex)
        {
                m_pMutex=pMutex;
                pMutex->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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

字正腔圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值