zoukankan      html  css  js  c++  java
  • 转载:CreateMutex WaitForSingleObject ReleaseMutex使用

    HANDLE CreateMutex(

     LPSECURITY_ATTRIBUTES lpMutexAttributes,//

     BOOL bInitialOwner,  // flag for initial ownership

     LPCTSTR lpName     // pointer to mutex-object name

     );

    参数2:指示互斥对象的初始拥有者。 如果该值是真,调用者创建互斥对象,调用的线程获得互斥对象的所有权。 否则,不拥有所有权,此时互斥对象处于空闲状态,其他线程可以占用。

    (-)  主线程中创建拥有所有权的互斥量,两个子线程中分别等待互斥量-》没有输出

     1 DWORD WINAPI ThreadProc1(LPVOID lpParameter);
     2 DWORD WINAPI ThreadProc2(LPVOID lpParameter);
     3 
     4 int    ticket = 50;
     5 HANDLE hMutex = NULL;
     6 
     7 int _tmain(int argc, _TCHAR* argv[])
     8 {
     9     HANDLE handle1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
    10     HANDLE handle2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
    11 
    12     CloseHandle(handle1);
    13     CloseHandle(handle2);
    14 
    15     hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态
    16 
    17     Sleep(4000);
    18 
    19     return 0;
    20 }
    21 
    22 DWORD WINAPI ThreadProc1(LPVOID lpParameter)
    23 {
    24 
    25     //WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象
    26     while(TRUE)
    27     {
    28 
    29         WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象
    30 
    31         if ( ticket > 0 )
    32         {
    33             Sleep(1);
    34 
    35             printf("thread1 sale the ticket id is: %d
    ", ticket--);
    36         }
    37         else
    38         {
    39             break;
    40         }                 
    41 
    42         ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
    43     }
    44     //ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
    45     return 0;
    46 } 
    47 
    48 DWORD WINAPI ThreadProc2(LPVOID lpParameter)
    49 {
    50 
    51     while(TRUE)
    52     {
    53 
    54         WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象         
    55 
    56         if ( ticket > 0 )
    57         {
    58 
    59            Sleep(1);
    60 
    61            printf("thread2 sale the ticket id is: %d
    ", ticket--);
    62 
    63         }
    64         else
    65         {
    66             break;
    67         }        
    68 
    69         ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
    70     }
    71 
    72     return 0;
    73 }

    (二)  主线程中创建没有所有权的互斥量,两个子线程中分别等待互斥量-》输出如下

    thread1 sale the ticket id is: 50
    thread2 sale the ticket id is: 49
    thread1 sale the ticket id is: 48
    thread2 sale the ticket id is: 47
    thread1 sale the ticket id is: 46
    thread2 sale the ticket id is: 45
    thread1 sale the ticket id is: 44
    thread2 sale the ticket id is: 43
    thread1 sale the ticket id is: 42
    thread2 sale the ticket id is: 41
    thread1 sale the ticket id is: 40
    thread2 sale the ticket id is: 39
    thread1 sale the ticket id is: 38
    thread2 sale the ticket id is: 37
    thread1 sale the ticket id is: 36
    thread2 sale the ticket id is: 35
    thread1 sale the ticket id is: 34
    thread2 sale the ticket id is: 33
    thread1 sale the ticket id is: 32
    thread2 sale the ticket id is: 31
    thread1 sale the ticket id is: 30
    thread2 sale the ticket id is: 29
    thread1 sale the ticket id is: 28
    thread2 sale the ticket id is: 27
    thread1 sale the ticket id is: 26
    thread2 sale the ticket id is: 25
    thread1 sale the ticket id is: 24
    thread2 sale the ticket id is: 23
    thread1 sale the ticket id is: 22
    thread2 sale the ticket id is: 21
    thread1 sale the ticket id is: 20
    thread2 sale the ticket id is: 19
    thread1 sale the ticket id is: 18
    thread2 sale the ticket id is: 17
    thread1 sale the ticket id is: 16
    thread2 sale the ticket id is: 15
    thread1 sale the ticket id is: 14
    thread2 sale the ticket id is: 13
    thread1 sale the ticket id is: 12
    thread2 sale the ticket id is: 11
    thread1 sale the ticket id is: 10
    thread2 sale the ticket id is: 9
    thread1 sale the ticket id is: 8
    thread2 sale the ticket id is: 7
    thread1 sale the ticket id is: 6
    thread2 sale the ticket id is: 5
    thread1 sale the ticket id is: 4
    thread2 sale the ticket id is: 3
    thread1 sale the ticket id is: 2
    thread2 sale the ticket id is: 1

    (三)  主线程中创建没有所有权的互斥量,主线程和子线程中分别等待互斥量-》主线程和子线程交替输出

    (四)  主线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有主线程输出

    这个原因不知道如何解释,难道在主线程中创建有所有权的,其他线程就永远等待不到了吗

    (五) 子线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有子线程输出

    (四)和(五)的结果可以说明在哪个线程中创建拥有所有权的互斥量,所有权永远被此线程占有,即使释放了互斥量。

    以上结果都在Wince6.0测试。

    后来找到一个 说明,貌似可以说明以上结论呢:

    如创建进程希望立即拥有互斥体,则设为TRUE。一个互斥体同时只能由一个线程拥有。是FALSE,表示刚刚创建的这个Mutex不属于任何线程 也就是没有任何线程拥有他,一个Mutex在没有任何线程拥有他的时候,他是处于激发态的, 所以处于有信号状态。

  • 相关阅读:
    程序员必备工具之Cmder
    Markdown简明教程
    几种黑灰名词解释
    windows下 mysql启动错误1067进程意外终止
    Mac OS 安装redis
    java里的这些
    maven 两条命令建立 web项目
    判断StringBuilder是否为空
    map与list的交集、补集操作
    关于创业
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/5729495.html
Copyright © 2011-2022 走看看