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 = ;
  5. HANDLE hMutex = NULL;
  6.  
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. HANDLE handle1 = CreateThread(NULL,,ThreadProc1,NULL,,NULL);
  10. HANDLE handle2 = CreateThread(NULL,,ThreadProc2,NULL,,NULL);
  11.  
  12. CloseHandle(handle1);
  13. CloseHandle(handle2);
  14.  
  15. hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态
  16.  
  17. Sleep();
  18.  
  19. return ;
  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 > )
  32. {
  33. Sleep();
  34.  
  35. printf("thread1 sale the ticket id is: %d\n", ticket--);
  36. }
  37. else
  38. {
  39. break;
  40. }
  41.  
  42. ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
  43. }
  44. //ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
  45. return ;
  46. }
  47.  
  48. DWORD WINAPI ThreadProc2(LPVOID lpParameter)
  49. {
  50.  
  51. while(TRUE)
  52. {
  53.  
  54. WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象
  55.  
  56. if ( ticket > )
  57. {
  58.  
  59. Sleep();
  60.  
  61. printf("thread2 sale the ticket id is: %d\n", ticket--);
  62.  
  63. }
  64. else
  65. {
  66. break;
  67. }
  68.  
  69. ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
  70. }
  71.  
  72. return ;
  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在没有任何线程拥有他的时候,他是处于激发态的, 所以处于有信号状态。

转载:CreateMutex WaitForSingleObject ReleaseMutex使用的更多相关文章

  1. CreateMutex() 、ReleaseMutex()

    功能: CreateMutex() 用于有独占要求的程序 (在其进程运行期间不允许其他使用此端口设备的程序运行,或不允许同名程序运行). 比如运行金山词霸时,一次只能运行一个实例,当运行第二个实例时, ...

  2. 零基础逆向工程38_Win32_12_信号量_线程控制小结

    1 信号量 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施,是可以用来保证两个或多个关键代码段不被并发调用.[百度百科] 1.1 创建信号量 HANDLE Create ...

  3. 事件EVENT与waitforsingleobject的使用以及Mutex与Event的区别

    Mutex与Event控制互斥事件的使用详解 最近写一程序,误用了Mutex的功能,错把Mutex当Event用了. [Mutex] 使用Mutex的主要函数:CreateMutex.ReleaseM ...

  4. windows多线程同步--互斥量

    关于互斥量的基本概念:百度百科互斥量 推荐参考博客:秒杀多线程第七篇 经典线程同步 互斥量Mutex 注意:互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...

  5. 如何创建一个简单的C++同步锁框架(译)

    翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...

  6. 多线程编程之Windows同步方式

    在Windows环境下针对多线程同步与互斥操作的支持,主要包括四种方式:临界区(CriticalSection).互斥对象(Mutex).信号量(Semaphore).事件对象(Event).下面分别 ...

  7. c++一些面试题目

    1.What is achieved by prefixing the 'static' keyword to a file-level function or file-level variable ...

  8. Windows核心编程:第9章 用内核对象进行线程同步

    Github https://github.com/gongluck/Windows-Core-Program.git //第9章 用内核对象进行线程同步.cpp: 定义应用程序的入口点. // #i ...

  9. 多线程的那点儿事(之windows锁)

    在windows系统中,系统本身为我们提供了很多锁.通过这些锁的使用,一方面可以加强我们对锁的认识,另外一方面可以提高代码的性能和健壮性.常用的锁以下四种:临界区,互斥量,信号量,event. (1) ...

随机推荐

  1. MySQL存储过程(批量生成论坛中发帖、回帖、主题等数据)

    USE 数据库名称1;DROP PROCEDURE IF EXISTS 数据库名称1.存储过程名称;delimiter $$CREATE PROCEDURE 数据库名称1.存储过程名称(in v_co ...

  2. 编写Robotium测试程序

    6.编写Robotium测试程序 1)导包 //导入需要测试的工程 import com.example.android.notepad.NotesList; //robotium提供的测试用类 im ...

  3. Docker镜像的目录存储讲解

    我们成功安装完docker后,执行命令行sudo docker run hello-world, 如果是第一次执行,则会从远程拉取hello-world的镜像到本地,然后运行,显示hello worl ...

  4. JavaScript实现的水果忍者游戏,支持鼠标操作

    智能手机刚刚普及时,水果忍者这款小游戏可谓风靡一时.几年过去了,现在,让我们用纯JavaScript来实现这个水果忍者游戏,就算是为了锤炼我们的JavaScript开发技能吧. 大家可以通过这个链接在 ...

  5. R文件报错的原因

    一般R文件报错,无非是资源文件错误,图片命名错误,但是编译都会报错,可以很快解决.但是前几天,引入一个第三方aar包后,项目编译正确,但是就是R文件报错,找不到R文件,整个项目一片报红. 1)首先编译 ...

  6. Google Colab的一些注意事项

    1.执行命令行前面加! 当我们使用python解释器时,我们需要不停地在命令行和IDE 之间切换,当我们需要使用命令行工具时.不过,Jupyter Notebook给了我们在notebook中运行sh ...

  7. 第009课 gcc和arm-linux-gcc和MakeFile

    from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...

  8. Hibernate的二级缓存使用(spring使用)

    (一)Hibernate的二级缓存策略的一般过程如下: 1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库 ...

  9. 正则表达式匹配:根据key获取value

    需求 url请求html字符串,dytk值写在js里,可以看成是key-value的格式,需要提取dytk值. 解决方法 正则匹配 private string get_dytk(string htm ...

  10. java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏

    增加的菜单栏效果图如下: eclipse 中调整到 swt的design视图下 控件区域选择Menu Controls 将Menu Bar拖动到窗口标题栏 将Cascaded Menu拖动到Menu ...