#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;
}