用C++和Windows的互斥对象(Mutex)来实现线程同步锁

//这是2个线程模拟卖火车票的小程序
#include 
#include 

DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data

int index=0;
int tickets=10;
HANDLE hMutex;
void main()
{
    HANDLE hThread1;
    HANDLE hThread2;
    //创建线程

    hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
    CloseHandle(hThread1);
    CloseHandle(hThread2);

    //创建互斥对象
    hMutex=CreateMutex(NULL,TRUE,"tickets");
    if (hMutex)
    {
        if (ERROR_ALREADY_EXISTS==GetLastError())
        {
            cout<<"only one instance can run!"<0)
        {
            Sleep(1);
            cout<<"thread1 sell ticket :"<0)
        {
            Sleep(1);
            cout<<"thread2 sell ticket :"<

//上面的例子是基于互斥对象的,这个是基于事件对象的
#include 
#include 

DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data

int tickets=100;
HANDLE g_hEvent;
void main()
{
    HANDLE hThread1;
    HANDLE hThread2;
    //创建人工重置事件内核对象
    g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets");
    if (g_hEvent)
    {
        if (ERROR_ALREADY_EXISTS==GetLastError())
        {
            cout<<"only one instance can run!"<0)
        {
            Sleep(1);
            cout<<"thread1 sell ticket :"<0)
        {
            Sleep(1);
            cout<<"thread2 sell ticket :"<


 

 

准备知识:1,内核对象互斥体(Mutex)的工作机理,WaitForSingleObject函数的用法,这些可以从MSDN获取详情;2,当两个或 更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。

下边是我参考开源项目C++ Sockets的代码,写的线程锁类

Lock.h

?
#ifndef _Lock_H
#define _Lock_H 
#include  
//锁接口类
class IMyLock
{
public :
    virtual ~IMyLock() {} 
    virtual void Lock() const = 0;
    virtual void Unlock() const = 0;
}; 
//互斥对象锁类
class Mutex : public IMyLock
{
public :
    Mutex();
    ~Mutex(); 
    virtual void Lock() const ;
    virtual void Unlock() const
private :
    HANDLE m_mutex;
}; 
//锁
class CLock
{
public :
    CLock( const IMyLock&);
    ~CLock(); 
private :
    const IMyLock& m_lock;
};
#endif

Lock.cpp

?
#include "Lock.h" 
//创建一个匿名互斥对象
Mutex::Mutex()
{
    m_mutex = ::CreateMutex(NULL, FALSE, NULL);
}
//销毁互斥对象,释放资源
Mutex::~Mutex()
{
    ::CloseHandle(m_mutex);
//确保拥有互斥对象的线程对被保护资源的独自访问
void Mutex::Lock() const
{
    DWORD d = WaitForSingleObject(m_mutex, INFINITE);
//释放当前线程拥有的互斥对象,以使其它线程可以拥有互斥对象,对被保护资源进行访问
void Mutex::Unlock() const
{
    ::ReleaseMutex(m_mutex);
//利用C++特性,进行自动加锁
CLock::CLock( const IMyLock& m) : m_lock(m)
{
    m_lock.Lock();
//利用C++特性,进行自动解锁
CLock::~CLock()
{
    m_lock.Unlock();
}

下边是测试代码MyLock.cpp

?
// MyLock.cpp : 定义控制台应用程序的入口点。
// 
#include
#include
#include "Lock.h"
using namespace std; 
//创建一个互斥对象
Mutex g_Lock; 
//线程函数
unsigned int __stdcall StartThread( void *pParam)
{
    char *pMsg = ( char *)pParam;
    if (!pMsg)
    {
        return (unsigned int )1;
   
    //对被保护资源(以下打印语句)自动加锁
    //线程函数结束前,自动解锁
    CLock lock(g_Lock);
    for ( int i = 0; i < 5; i++ )
    {
        cout << pMsg << endl;
        Sleep( 500 );
    }
    return (unsigned int )0;
int main( int argc, char * argv[])
{
    HANDLE hThread1, hThread2;
    unsigned int uiThreadId1, uiThreadId2; 
    char *pMsg1 = "First print thread." ;
    char *pMsg2 = "Second print thread."
    //创建两个工作线程,分别打印不同的消息
    //hThread1 = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartThread, (void *)pMsg1, 0, (LPDWORD)&uiThreadId1);
    //hThread2 = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartThread, (void *)pMsg2, 0, (LPDWORD)&uiThreadId2); 
    hThread1 = ( HANDLE )_beginthreadex(NULL, 0, &StartThread, ( void *)pMsg1, 0, &uiThreadId1);
    hThread2 = ( HANDLE )_beginthreadex(NULL, 0, &StartThread, ( void *)pMsg2, 0, &uiThreadId2); 
    //等待线程结束
    DWORD dwRet = WaitForSingleObject(hThread1,INFINITE);
    if ( dwRet == WAIT_TIMEOUT )
    {
        TerminateThread(hThread1,0);
    }
    dwRet = WaitForSingleObject(hThread2,INFINITE);
    if ( dwRet == WAIT_TIMEOUT )
    {
        TerminateThread(hThread2,0);
    }
    //关闭线程句柄,释放资源
    ::CloseHandle(hThread1);
    ::CloseHandle(hThread2);
    system ( "pause" );
    return 0;
}
 
一个实用的mutex类,可以直接应用与工程
 
#include "mutex.h"
02  
03 namespace ZFPT
04 {
05     CMutex::CMutex()
06     {
07         pthread_mutex_init(&m_Mutex, NULL);
08     }
09  
10     CMutex::~CMutex()
11     {
12         pthread_mutex_destroy(&m_Mutex);
13     }
14  
15     intCMutex::lock()
16     {
17         returnpthread_mutex_lock(&m_Mutex);
18     }
19  
20     intCMutex::tryLock()
21     {
22         returnpthread_mutex_trylock(&m_Mutex);
23     }
24  
25     intCMutex::unLock()
26     {
27         returnpthread_mutex_unlock(&m_Mutex);
28     }
29  
30     CScopeLock::CScopeLock(CMutex& cMutex,bool IsTry): m_Mutex(cMutex)
31     {
32         if(IsTry)
33         {
34             m_Mutex.tryLock();
35         }
36         else
37         {
38             m_Mutex.lock();
39         }
40     }
41  
42     CScopeLock::~CScopeLock()
43     {
44         m_Mutex.unLock();
45     }
46  
47  
48  
49 }
 
 

你可能感兴趣的:(C++,windows)