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

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

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

Lock.h

#ifndef _Lock_H 
#define _Lock_H  

#include <windows.h>  

//锁接口类 
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 <iostream> 
#include <process.h> 
#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; 
} 

 

 

posted @ 2012-06-07 10:07  Luochengor  阅读(4211)  评论(0编辑  收藏  举报