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

来源:互联网 发布:科比10年总决赛数据 编辑:程序博客网 时间:2024/06/16 20:33

http://blog.csdn.net/jiangxinyu/article/details/7754573


[csharp] view plain copy
  1. //这是2个线程模拟卖火车票的小程序  
  2. #include <windows.h>  
  3. #include <iostream.h>  
  4.   
  5. DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data  
  6. DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data  
  7.   
  8. int index=0;  
  9. int tickets=10;  
  10. HANDLE hMutex;  
  11. void main()  
  12. {  
  13.     HANDLE hThread1;  
  14.     HANDLE hThread2;  
  15.     //创建线程  
  16.   
  17.     hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);  
  18.     hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);  
  19.     CloseHandle(hThread1);  
  20.     CloseHandle(hThread2);  
  21.   
  22.     //创建互斥对象  
  23.     hMutex=CreateMutex(NULL,TRUE,"tickets");  
  24.     if (hMutex)  
  25.     {  
  26.         if (ERROR_ALREADY_EXISTS==GetLastError())  
  27.         {  
  28.             cout<<"only one instance can run!"<<endl;  
  29.             return;  
  30.         }  
  31.     }  
  32.     WaitForSingleObject(hMutex,INFINITE);  
  33.     ReleaseMutex(hMutex);  
  34.     ReleaseMutex(hMutex);  
  35.       
  36.     Sleep(4000);  
  37. }  
  38. //线程1的入口函数  
  39. DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data  
  40. {  
  41.     while (true)  
  42.     {  
  43.         ReleaseMutex(hMutex);  
  44.         WaitForSingleObject(hMutex,INFINITE);  
  45.         if (tickets>0)  
  46.         {  
  47.             Sleep(1);  
  48.             cout<<"thread1 sell ticket :"<<tickets--<<endl;  
  49.         }  
  50.         else  
  51.             break;  
  52.         ReleaseMutex(hMutex);  
  53.     }  
  54.   
  55.     return 0;  
  56. }  
  57. //线程2的入口函数  
  58. DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data  
  59. {  
  60.     while (true)  
  61.     {  
  62.         ReleaseMutex(hMutex);  
  63.         WaitForSingleObject(hMutex,INFINITE);  
  64.         if (tickets>0)  
  65.         {  
  66.             Sleep(1);  
  67.             cout<<"thread2 sell ticket :"<<tickets--<<endl;  
  68.         }  
  69.         else  
  70.             break;  
  71.         ReleaseMutex(hMutex);  
  72.     }  
  73.       
  74.     return 0;  
  75. }  

[csharp] view plain copy
  1. //上面的例子是基于互斥对象的,这个是基于事件对象的  
  2. #include <windows.h>  
  3. #include <iostream.h>  
  4.   
  5. DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data  
  6. DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data  
  7.   
  8. int tickets=100;  
  9. HANDLE g_hEvent;  
  10. void main()  
  11. {  
  12.     HANDLE hThread1;  
  13.     HANDLE hThread2;  
  14.     //创建人工重置事件内核对象  
  15.     g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets");  
  16.     if (g_hEvent)  
  17.     {  
  18.         if (ERROR_ALREADY_EXISTS==GetLastError())  
  19.         {  
  20.             cout<<"only one instance can run!"<<endl;  
  21.             return;  
  22.         }  
  23.     }  
  24.     SetEvent(g_hEvent);  
  25.   
  26.     //创建线程  
  27.     hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);  
  28.     hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);  
  29.     CloseHandle(hThread1);  
  30.     CloseHandle(hThread2);  
  31.       
  32.     //让主线程睡眠4秒  
  33.     Sleep(4000);  
  34.     //关闭事件对象句柄  
  35.     CloseHandle(g_hEvent);  
  36. }  
  37. //线程1的入口函数  
  38. DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data  
  39. {  
  40.     while (true)  
  41.     {  
  42.         WaitForSingleObject(g_hEvent,INFINITE);  
  43.         //ResetEvent(g_hEvent);  
  44.         if (tickets>0)  
  45.         {  
  46.             Sleep(1);  
  47.             cout<<"thread1 sell ticket :"<<tickets--<<endl;  
  48.             SetEvent(g_hEvent);  
  49.         }  
  50.         else  
  51.         {  
  52.             SetEvent(g_hEvent);  
  53.             break;  
  54.         }  
  55.     }  
  56.       
  57.     return 0;  
  58. }  
  59. //线程2的入口函数  
  60. DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data  
  61. {  
  62.     while (true)  
  63.     {  
  64.         //请求事件对象  
  65.         WaitForSingleObject(g_hEvent,INFINITE);  
  66.         //ResetEvent(g_hEvent);  
  67.         if (tickets>0)  
  68.         {  
  69.             Sleep(1);  
  70.             cout<<"thread2 sell ticket :"<<tickets--<<endl;  
  71.             SetEvent(g_hEvent);  
  72.         }  
  73.         else  
  74.         {  
  75.             SetEvent(g_hEvent);  
  76.             break;  
  77.         }  
  78.     }  
  79.   
  80.     return 0;  
  81. }  


 

 

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

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

Lock.h

?
#ifndef _Lock_H
#define _Lock_H 
#include <windows.h> 
//锁接口类
classIMyLock
{
public:
    virtual~IMyLock() {} 
    virtualvoidLock()const= 0;
    virtualvoidUnlock()const= 0;
}; 
//互斥对象锁类
classMutex :publicIMyLock
{
public:
    Mutex();
    ~Mutex(); 
    virtualvoidLock()const;
    virtualvoidUnlock()const
private:
    HANDLEm_mutex;
}; 
//锁
classCLock
{
public:
    CLock(constIMyLock&);
    ~CLock(); 
private:
    constIMyLock& m_lock;
};
#endif

Lock.cpp

?
#include "Lock.h" 
//创建一个匿名互斥对象
Mutex::Mutex()
{
    m_mutex = ::CreateMutex(NULL, FALSE, NULL);
}
//销毁互斥对象,释放资源
Mutex::~Mutex()
{
    ::CloseHandle(m_mutex);
//确保拥有互斥对象的线程对被保护资源的独自访问
voidMutex::Lock()const
{
    DWORDd = WaitForSingleObject(m_mutex, INFINITE);
//释放当前线程拥有的互斥对象,以使其它线程可以拥有互斥对象,对被保护资源进行访问
voidMutex::Unlock()const
{
    ::ReleaseMutex(m_mutex);
//利用C++特性,进行自动加锁
CLock::CLock(constIMyLock& 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"
usingnamespacestd; 
//创建一个互斥对象
Mutex g_Lock; 
//线程函数
unsigned int__stdcall StartThread(void*pParam)
{
    char*pMsg = (char*)pParam;
    if(!pMsg)
    {
        return(unsignedint)1;
   
    //对被保护资源(以下打印语句)自动加锁
    //线程函数结束前,自动解锁
    CLock lock(g_Lock);
    for(inti = 0; i < 5; i++ )
    {
        cout << pMsg << endl;
        Sleep( 500 );
    }
    return(unsignedint)0;
int main(int argc,char* argv[])
{
    HANDLEhThread1, hThread2;
    unsignedintuiThreadId1, 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); 
    //等待线程结束
    DWORDdwRet = 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");
    return0;
}
 
一个实用的mutex类,可以直接应用与工程
 
#include "mutex.h"
02 
03namespace 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,boolIsTry): 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}
 
 

阅读全文
0 0
原创粉丝点击