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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值