HANDLE CreateMutex(

LPSECURITY_ATTRIBUTES lpMutexAttributes,//

BOOL bInitialOwner,  // flag for initial ownership

LPCTSTR lpName     // pointer to mutex-object name

);

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

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

DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);int    ticket = 50;
HANDLE hMutex = NULL;int _tmain(int argc, _TCHAR* argv[])
{HANDLE handle1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);HANDLE handle2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);CloseHandle(handle1);CloseHandle(handle2);hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态Sleep(4000);return 0;
}DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{//WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象while(TRUE){WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象if ( ticket > 0 ){Sleep(1);printf("thread1 sale the ticket id is: %d\n", ticket--);}else{break;}                 ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统}//ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统return 0;
} DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{while(TRUE){WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象         if ( ticket > 0 ){Sleep(1);printf("thread2 sale the ticket id is: %d\n", ticket--);}else{break;}        ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统}return 0;
}

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

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. windows线程 互斥锁CreateMutex、ReleaseMutex、CloseHandle

    互斥 相关问题 多线程下代码或资源的共享使用. 互斥的使用 1.创建互斥 HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes,//安 ...

  2. CreateMutex() 、ReleaseMutex()

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

  3. 【转】WaitForSingleObject() ReleaseMutex()的用法

    一,WaitForSingleObject的用法 1.WaitForSingleObject 的用法 DWORD WaitForSingleObject(                      H ...

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

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

  5. MFC第三节-多线程

    一.程序,进程,线程 程序是指令的集合,以文件形式储存在磁盘上.一个程序可以对应多个进程,一个进程代表一个实例. 进程由管理进程的内核对象.地址空间组成.内核对象存放关于进程的统计信息,地址空间包含可 ...

  6. C++编程模拟生产者消费者模型

    生产者消费者问题是操作系统中典型的进程同步互斥问题,(英语:Producer-Consumer problem),也称有限缓冲问题(英语:Bounded-buffer problem),是一个多线程同 ...

  7. Windows 命名管道 + 异步I/O模型

    一.管道 + 异步I/O 本地通讯模型 1.客户端操作 发送请求 (1) 互斥上锁 CreateMutex, WaitForSingleObject (2) 打开命名管道 OpenNamedPipe ...

  8. 秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量

    前面<秒杀多线程第四篇一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,这个问题包括了主线程与子线程的同步,子线程间的互斥,是一道非常经典的多线程同步互斥问题范例,后面分别用了 ...

  9. windows系统c++多线程开发

    线程的一些基本概念 一.线程的基本概念. 基本概念:线程,即轻量级进程(LWP:LightWeight Process),是程序执行流的最小单元.一个标准的线程由线程ID.当前指令指针(PC),寄存器 ...

最新文章

  1. 日本“AI 鱼脸识别”项目,每分钟识别 100 条
  2. 基于注解的方式配置bean
  3. 极速理解设计模式系列:22.状态模式(State Pattern)
  4. 密码学加密算法分类_密码学中的国际数据加密算法(IDEA)
  5. 观看实验中微型计算机虚拟拆装演示,虚拟仿真实验 北斗一号微机原理虚拟仿真实验系统64位 v3.0...
  6. python3-matplotlib基本使用(以折线图为例)
  7. 模块设计之“模块”与“模块化”
  8. 枚举类型 实现一个enumeration对于类的加工的函数
  9. 云速建站_华为云域名专场钜惠,助推中小企业云速建站
  10. 在win7物理机,使用vmware,3台centos7系统,分别部署httpd,php-fpm,mariadb
  11. 成都-峨眉山(乐山)旅游攻略
  12. ipod nano 无法添加mp4视频 电影失败解决方法
  13. 10bit灰阶测试图_我可能买的是一块假10bit显示器以及一块假8bit显示器?
  14. css所有缩写属性,CSS常见属性缩写与全写对比
  15. PHP网页文件扩展名,网页文件的扩展名有哪些
  16. 第29课:AD中class,设计参数,规则的设置
  17. 小括号在c语言的作用,C语言的小括号----其实是逗号运算符
  18. ubuntu的cuda10和cuda11共存
  19. 单纯p2p理财模式难以发展壮大和长久存在!
  20. 阻容感基础06:电容器分类(1)-陶瓷电容

热门文章

  1. 程序员还在用360,腾讯电脑管家清理注册表,清理垃圾?只能说你太low
  2. 新品疑似违反开源协议,TikTok被海外网友举报
  3. 联想服务器显示器优点,联想ThinkVision S23d的16:10屏幕在办公方面优势明显_联想 ThinkVision S23d_液晶显示器评测-中关村在线...
  4. mac high sierra开机按option怎么只有一个磁盘_mac下载工具----SiteSucker
  5. tp3数据表新加了一个字段,怎么插都插不进去
  6. 对自己的检讨与后续期望
  7. lxf-spring开发
  8. Python3网络设备巡检(交换机篇)
  9. 图的最短路径--单源、多源最短路径
  10. 素数(质数)判断方法