windows&linux多線程互斥

///////////////////////////////////////////   begin of CGlobal.h ////////////////////////////////////////////////

///> filename: CGlobal.h

#ifndef CGLOBAL_H

#define CGLOBAL_H


#include "CMutex.h"

class CGlobal
{
public:
static int num;
static CMutex mutex;
public:
static void set( int val );
//{
// num = val;
//}
static int get();
//{
// return num;
//}
static void add();
};

#endif

///////////////////////////////////////////   end of CGlobal.h ////////////////////////////////////////////////



///////////////////////////////////////////   begin of CGlobal.cpp ////////////////////////////////////////////////

///> filename: CGlobal.cpp

#include "CGlobal.h"

int CGlobal::num = 0; 
CMutex CGlobal::mutex;

void CGlobal::set( int val )
{
num = val;
}

int CGlobal::get()
{
return num;
}

void CGlobal::add()
{
num++;
printf("numval:%d\n", num);
}

///////////////////////////////////////////   end of CGlobal.cpp ////////////////////////////////////////////////


///////////////////////////////////////////   begin of CMutex.h ////////////////////////////////////////////////

///filename: CMutex.h
#ifndef CMUTEX_H
#define CMUTEX_H

#include <stdio.h>

#ifdef WIN32
#include <windows.h>
#include <process.h>
#endif

#ifndef WIN32
#include<pthread.h>
#endif

///> platform for windows
#ifdef WIN32
class CMutex
{
public:
CMutex()
{
//_critsection = CreateMutex(NULL, FALSE, NULL);
InitializeCriticalSection(&_critsection);
}
virtual ~CMutex()
{
DeleteCriticalSection(&_critsection);
}
void AcquireMutex()
{
EnterCriticalSection(&_critsection);
}
void ReleaseMutex()
{
LeaveCriticalSection(&_critsection);
}
private:
CRITICAL_SECTION _critsection;
};

///> platform for linux
#else
class CMutex
{
public:
CMutex()
{
pthread_mutex_init(&_mtx, NULL); 
}
~CMutex()
{
pthread_mutex_destroy(&_mtx); 
}
void AcquireMutex()
{
pthread_mutex_lock(&_mtx);
}
void ReleaseMutex()
{
pthread_mutex_unlock(&_mtx);
}
private:
pthread_mutex_t _mtx;
};

#endif

#endif

///////////////////////////////////////////   end of CMutex.h ////////////////////////////////////////////////




///////////////////////////////////////////   begin of CMutexGuard.h ////////////////////////////////////////////////

///> filename: CMutexGuard.h
#ifndef CMUTEX_GUARD_H
#define CMUTEX_GUARD_H

#include "CMutex.h"

class CMutexGuard
{
private:
CMutex& _mutex;
public:
explicit CMutexGuard(CMutex& mutex): _mutex(mutex)
{
_mutex.AcquireMutex();
}
~CMutexGuard()
{
_mutex.ReleaseMutex();
}
};

#endif

///////////////////////////////////////////   end of CMutexGuard.h ////////////////////////////////////////////////



///////////////////////////////////////////   begin of CThreadFun.h ////////////////////////////////////////////////

///> filename: CThreadFun.h

#ifndef CMINE_THREAD_FUN_H
#define CMINE_THREAD_FUN_H


#ifdef WIN32
#include<windows.h>
#endif

///> windows platform
#ifdef WIN32
class CMineThreadFun
{
public:
static DWORD* ThreadProc( LPVOID lpParameter );
};

///> other platform
#else
class CMineThreadFun
{
static void* ThreadProc(void* param);
};

#endif

#endif

///////////////////////////////////////////   end of CThreadFun.h ////////////////////////////////////////////////



///////////////////////////////////////////   begin of CThreadFun.cpp ////////////////////////////////////////////////

///> filename: CThreadFun.cpp
#include "CThreadFun.h"
#include "CMutexGuard.h"
#include "CGlobal.h"

#ifdef WIN32
DWORD* CMineThreadFun::ThreadProc( LPVOID lpParameter)
{
CMutexGuard guard(CGlobal::mutex);
while(true)
{
CGlobal::add();
//Sleep(500);
}
return NULL;
}
#endif

#ifndef WIN32
void* CMineThreadFun::ThreadProc(void* threadParam)
{
CMutexGuard guard(CGlobalMutex::mutex);
while(true)
{
CGlobal::add();
}
return ((void *)0); 
}
#endif

///////////////////////////////////////////   end of CThreadFun.cpp ////////////////////////////////////////////////



///////////////////////////////////////////   begin of main.cpp ////////////////////////////////////////////////

///> filename: main.cpp

#include <stdio.h>
#include "classtemp.h"
#include "CThreadFun.h"

int main()
{
///> windows platform
#ifdef WIN32
HANDLE hThread[2];
for( int i=0; i< sizeof(hThread)/sizeof(hThread[0]); i++ )
{
hThread[i] = ::CreateThread( NULL, 0, (PTHREAD_START_ROUTINE)CMineThreadFun::ThreadProc, NULL, 0, NULL );
}
::WaitForMultipleObjects( sizeof(hThread)/sizeof(hThread[0]), hThread, TRUE, INFINITE ); 
for( int i=0; i< sizeof(hThread)/sizeof(hThread[0]); i++ )
{
::CloseHandle( hThread[i] );
}
#endif

///> linux platform
#ifndef WIN32
pthread_t threadnum[2];
for( int i=0; i< sizeof(threadnum)/sizeof(threadnum[0]); i++ )
{
pthread_create(&threadnum[i], NULL, CMineThreadFun::ThreadProc, NULL); 
}
for( int i=0; i< sizeof(threadnum)/sizeof(threadnum[0]); i++ )
{
pthread_join(thread[i],NULL);
}
#endif

return 0;
}

///////////////////////////////////////////   end of main.cpp ////////////////////////////////////////////////





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章