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多线程同步
概述 任何单个应用程序都不能完全使该处理器达到满负荷.当一个线程遇到较长等待时间事件时,同步多线程还允许另一线程中的指令使用所有执行单元.例如,当一个线程发生高速缓存不命中,另一个线程可以继续执行.同 ...
随机推荐
- yumdebug
- setOpaque(true);设置控件不透明
setOpaque(true);设置控件不透明setOpaque(false);设置控件透明
- [转载]Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结
转载:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...
- textChanged(*)重点
# -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...
- UITableView使用总结和性能优化
UITableView使用总结和性能优化 UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.如 果我们查看UITabl ...
- UCTF Final-Hackventure
抽出世间将UCTF Final中的hackventure给记录下,算是个总结.题目是有一个游戏,游戏地图是随机生成的,用户可以攻打Server,如果3个Server都被攻占的话,那么用户就赢了,但是并 ...
- winform窗体中查找控件
private RichTextBox FindControl() { RichTextBox ret = null; try { ...
- SQL server根据值搜表名和字段
DECLARE @what varchar(800) SET @what='lll' --要搜索的字符串 DECLARE @sql varchar(8000) DECLARE TableCursor ...
- PHP学习笔记十五【面向对象二】
<?php class Cat{ //public 访问修饰符 public $name; public $age; } //创建 $cat1=new Cat; $cat1->name=& ...
- sqlite3---代码操作
1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...