windows&linux多线程互斥
来源:互联网 发布:linux c语言多线程 编辑:程序博客网 时间:2024/06/05 16:55
/////////////////////////////////////////// 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 ////////////////////////////////////////////////
#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 ////////////////////////////////////////////////
#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 ////////////////////////////////////////////////
#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 ////////////////////////////////////////////////
#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 ////////////////////////////////////////////////
- windows&linux多线程互斥
- 多线程临界区(windows)/互斥(linux)
- Windows多线程的同步与互斥
- Windows多线程中的互斥对象
- Windows多线程的同步与互斥
- windows多线程同步互斥--总结
- windows多线程同步和互斥关系
- windows 多线程 同步与互斥
- 【Linux】 多线程编程(互斥锁实现互斥)
- linux 多线程编程 同步与互斥
- linux多线程的互斥与同步
- linux多线程编程 同步与互斥
- linux c++ 多线程 互斥访问
- Linux C 多线程【4】--线程互斥
- linux 多线程学习一:互斥
- Linux多线程编程--同步与互斥
- Linux多线程编程--同步与互斥
- Linux多线程的同步与互斥
- oracle 限制用户密码输入错误次数
- javascript 思维导图
- C语言基础--常用字符串处理函数
- java中String的七种用法
- linux下对端口的操作
- windows&linux多线程互斥
- BZOJ3451 Tyvj1953 Normal
- 算法竞赛入门经典第二版第一章语言篇
- unity进程间通信
- 减少页面加载时间的方法。(加载时间指感知的时间或者实际加载时间)
- ibatis连表查询出关联的所有数据
- myeclipse修改默认启动空间
- 【OVS2.5.0源码分析】nlattr数据结构分析
- 在TX1上安装caffe