Windows多线程同步系列之一-----互斥对象
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If
lpMutexAttributes is NULL, the handle cannot be inherited.
does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
lpName[in] Pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.
If lpName matches the name of an existing named mutex object, this function requests MUTEX_ALL_ACCESS access to the existing object. In this case, thebInitialOwner parameter is ignored because
it has already been set by the creating process. If the lpMutexAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.
If lpName is NULL, the mutex object is created without a name.
If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This
occurs because these objects share the same name space.
Terminal Services: The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space. The remainder of the name can contain any character except the backslash
character (\). For more information, see Kernel Object Name Spaces.
参数lpName指定是一个命名的互斥对象还是一个匿名的互斥对象,它们之间的区别我们留待后面再学习
该函数释放互斥对象的所有权,也就是说该函数将互斥对象置为有信号的状态该函数的参数即为CreateMutex函数返回的句柄对象
在利用互斥事件进行线程同步的时候我们还需要使用一个函数WaitForSingleObject该函数将等待互斥对象有状态才返回否则一直阻塞。
#include <windows.h>
#include <stdio.h> static int number=;
HANDLE Mutex; DWORD WINAPI ThreadOne(LPVOID lpParameter)
{
while()
{
WaitForSingleObject(Mutex,INFINITE);
if(number>)
{
printf("窗口1售出第%d张票...\n",number);
number--;
Sleep();
}
ReleaseMutex(Mutex);
}
return ;
}
DWORD WINAPI ThreadTwo(LPVOID lpParameter)
{
while()
{
WaitForSingleObject(Mutex,INFINITE);
if(number>)
{
printf("窗口2售出第%d张票...\n",number);
Sleep();
number--;
}
ReleaseMutex(Mutex);
}
return ;
} int main()
{
HANDLE HOne,HTwo;
Mutex=CreateMutex(NULL,FALSE,NULL);
printf("***********************vpoet******************\n");
HOne=CreateThread(NULL,,ThreadOne,NULL,,NULL);
printf("窗口1售票开始:\n");
HTwo=CreateThread(NULL,,ThreadTwo,NULL,,NULL);
printf("窗口2售票开始:\n");
CloseHandle(HOne);
CloseHandle(HTwo);
while(TRUE)
{
if(number==)
{
printf("不好意思,票卖完了!\n");
CloseHandle(Mutex);
return ;
}
else
{
continue;
}
} return ;
}
Windows多线程同步系列之一-----互斥对象的更多相关文章
- Windows多线程同步系列之三-----事件对象
事件是一个内核事件,内核事件是什么呢,我理解也不深入也不好说,暂且理解为一个内核维护的数据类型吧通过内核事件同步主要 的方法是对事件的信号有和无来进行同步. 比如当我们一个线程进入一段临界代码(独占代 ...
- Windows多线程同步系列之二-----关键区
关键区对象为:CRITICAL_SECTION 当某个线程进入关键区之后,其他线程将阻塞等待,知道该线程释放关键区的拥有权. 关键区同步主要有以下几个API 初始化关键区对象,无返回值,传入一个关键区 ...
- Windows多线程同步系列之四-----信号量
信号量说实话自己没怎么使用过.书上大概这样说,信号量设置一个资源访问计数.当该计数值大于0的时候,该信号量对象 为有信号状态,当该计数值等于0的时候,该信号量对象为无信号状态. 我们来查几个主要的AP ...
- 总结windows多线程同步互斥
windows多线程同步互斥--总结 我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同 ...
- windows多线程同步互斥--总结
我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同步--临界区 windows多线程同步 ...
- windows多线程同步总结
1.多线程同步与多线程互斥的关系 其实这也是我一直困扰的问题,在这里我只是说说我的理解.我的理解是多线程互斥是针对于多线程资源而言的. 而多线程同步是针对于多线程时序问题.由于线程的并发性导致其运行时 ...
- windows多线程同步--临界区
推荐参考博客:秒杀多线程第五篇 经典线程同步 关键段CS 关于临界区的观念,一般操作系统书上面都有. 适用范围:它只能同步一个进程中的线程,不能跨进程同步.一般用它来做单个进程内的代码快同步,效率 ...
- C++多线程同步之Mutex(互斥量)
原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...
- windows多线程同步
概述 任何单个应用程序都不能完全使该处理器达到满负荷.当一个线程遇到较长等待时间事件时,同步多线程还允许另一线程中的指令使用所有执行单元.例如,当一个线程发生高速缓存不命中,另一个线程可以继续执行.同 ...
随机推荐
- OSCLI
- Smart ——jiaoyou模板
<!--{foreach $vip_data as $key=>$volist}--> <!--{if $key==0 ||$key==1||$key==5||$key= ...
- C#中,表达式的计算遵循一个规律:从左到右依次计算。
int i = 0; int j = (i++)+(i++)=(i++)+i=i+++i=i+++i++=1;
- 河内塔(hanoi)
理论: 河内塔: 1.有三根杆子A,B,C.A杆上有若干碟子 2.每次移动一块碟子,小的只能叠在大的上面 3.把所有碟子从A杆全部移到C杆上 讲解: 设A上有n个盘子.如果n=1,则将圆盘从A直接 ...
- Top 15 Tools To Make Animated GIFs From Images & Video
Creating an animated GIF picture from photos or video with Adobe Photoshop is easy, but not everyone ...
- LR选择哪种方式录制
LR选择哪种方式录制,有以下考虑原则: 1.基于浏览器的应用程序推荐使用HTML-basic script方式录制 2.不是基于浏览器的应用程序推荐使用URL-basic script方式录制 3.如 ...
- js中json的转换
//aa='{"id":0,"appId":"app***********Id","appSecret":"a ...
- HTML5新增的拖放API---(一)
HTML5新增了关于拖放的API,通过API可以让HTML页面的任意元素都变成可拖动的,通过使用拖放机制可以开发出更友好的人机交互的界面. 拖放操作可以分为两个动作:在某个元素上按下鼠标移动鼠标(没有 ...
- elasticsearch,http://ip:9200访问不到的解决办法
现在解压elasticsearch之后,启动,通过http://localhost:9200可以访问的到,但是http://ip:9200访问不到,怎么办呢?带你解决,带你飞 修改elasticsea ...
- iOS 判断设备是否越狱
我们在开发过程中,需要知道设备是否越狱,在网上查看很多资料,为此封装一些判断的方法. 上代码,不解释: .h文件 #import <Foundation/Foundation.h> @in ...