多线程同步之互斥对象

作者:vpoet
mail:vpoet_sir@163.com

 
对卖票问题进行线程间同步,本文将在上文的基础上,使用互斥对象对线程进行同步。



首先看看windows API


该函数创建一个命名或者不命名的互斥对象


lpMutexAttributes
[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. 
参数lpMutexAttributes为互斥对象的安全属性,如果为NULL,则使用默认的安全属性

bInitialOwner[in] Specifies the initial owner of the mutex object. If this value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread
does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section. 
参数bInitialOwner指定该互斥对象的拥有者,若为TRUE则表明创建互斥对象的线程对其有所有权,而反之则表示创建互斥对象的线程不能对其拥有所有权

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该函数将等待互斥对象有状态才返回否则一直阻塞。

接下来我们来实际动手看看,注意在CreateMutex函数的第二个参数我们置为FALSE,因为我们需要在主线程中创建该互斥对象,但是把这个
互斥对象的所有权需要轮换的交给两个子线程交替的卖票。

代码如下:
#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 ;
}
运行结果:
嗯 没问题,达到我们要求了。

Ok,下次博文我们再一起学习使用事件对象进行线程间同步。

Windows多线程同步系列之一-----互斥对象的更多相关文章

  1. Windows多线程同步系列之三-----事件对象

    事件是一个内核事件,内核事件是什么呢,我理解也不深入也不好说,暂且理解为一个内核维护的数据类型吧通过内核事件同步主要 的方法是对事件的信号有和无来进行同步. 比如当我们一个线程进入一段临界代码(独占代 ...

  2. Windows多线程同步系列之二-----关键区

    关键区对象为:CRITICAL_SECTION 当某个线程进入关键区之后,其他线程将阻塞等待,知道该线程释放关键区的拥有权. 关键区同步主要有以下几个API 初始化关键区对象,无返回值,传入一个关键区 ...

  3. Windows多线程同步系列之四-----信号量

    信号量说实话自己没怎么使用过.书上大概这样说,信号量设置一个资源访问计数.当该计数值大于0的时候,该信号量对象 为有信号状态,当该计数值等于0的时候,该信号量对象为无信号状态. 我们来查几个主要的AP ...

  4. 总结windows多线程同步互斥

    windows多线程同步互斥--总结 我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同 ...

  5. windows多线程同步互斥--总结

    我的windows多线程系列文章: windows多线程--原子操作 windows多线程同步--事件 windows多线程同步--互斥量 windows多线程同步--临界区 windows多线程同步 ...

  6. windows多线程同步总结

    1.多线程同步与多线程互斥的关系 其实这也是我一直困扰的问题,在这里我只是说说我的理解.我的理解是多线程互斥是针对于多线程资源而言的. 而多线程同步是针对于多线程时序问题.由于线程的并发性导致其运行时 ...

  7. windows多线程同步--临界区

    推荐参考博客:秒杀多线程第五篇 经典线程同步 关键段CS   关于临界区的观念,一般操作系统书上面都有. 适用范围:它只能同步一个进程中的线程,不能跨进程同步.一般用它来做单个进程内的代码快同步,效率 ...

  8. C++多线程同步之Mutex(互斥量)

    原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...

  9. windows多线程同步

    概述 任何单个应用程序都不能完全使该处理器达到满负荷.当一个线程遇到较长等待时间事件时,同步多线程还允许另一线程中的指令使用所有执行单元.例如,当一个线程发生高速缓存不命中,另一个线程可以继续执行.同 ...

随机推荐

  1. IOS 监听通讯录是否改变

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRegist ...

  2. stagefright omx小结

    由于stagefright和openmax运行在两个不同的进程上,所以他们之间的通讯要经过Binder进行处理,本小结不考虑音频这一块,假设视频为MP4封装的AVC编码文件. 先简单的看一下stage ...

  3. Mysql 复制表结构 及其表的内容

    顺便转一下Mysql复制表结构.表数据的方法: 1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们 ...

  4. Entity Framework中实现查询的几种方法

    在介绍几种方法前,献上一张图,希望图的作者不要追究我的盗图之过.本文的内容是我自学时的笔记,自学的内容来自网络.手打的代码,切不可直接复制过去用,会有好多错别字什么的. Entity SQL 类似于S ...

  5. 获取UILabel上最后一个字符串的位置。获取文字长度和高度,自动换行

    //行的高度. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat ...

  6. (转) c++ 迭代器

    原地址:http://www.cnblogs.com/marchtea/archive/2012/02/27/2370068.html 前言: 以下的内容为我阅读c++沉思录18,19,20章的笔记以 ...

  7. (原)ubuntu上安装nvidia及torch的nccl

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5717234.html 参考网址: https://github.com/NVIDIA/nccl htt ...

  8. hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)

    绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...

  9. JSON 传值 textarea中虚拟换行功能

    遇到错误的袭击, 错误出现,使用jquery中ajax进行查询数据时执行完以后,需要把数据封装成为JSON类型的数据,并传递到前台去的时候出现Invalid JSON 错误,经查找后发现是在使用tex ...

  10. PK投票效果

    /** *createTime:2015-07-21 *updateTime:2015-06-22 *author:刘俊 *Description:PK投票 *phone:13469119119 ** ...