MSDN上的Mutex代码及其执行结果

#include <windows.h>
#include <stdio.h>#define THREADCOUNT 2HANDLE ghMutex; DWORD WINAPI WriteToDatabase( LPVOID );int main( void )
{HANDLE aThread[THREADCOUNT];DWORD ThreadID;int i;// Create a mutex with no initial owner
ghMutex = CreateMutex( NULL,              // default security attributesFALSE,             // initially not ownedNULL);             // unnamed mutexif (ghMutex == NULL) {printf("CreateMutex error: %d\n", GetLastError());return 1;}// Create worker threadsfor( i=0; i < THREADCOUNT; i++ ){aThread[i] = CreateThread( NULL,       // default security attributes0,          // default stack size
                     (LPTHREAD_START_ROUTINE) WriteToDatabase, NULL,       // no thread function arguments0,          // default creation flags&ThreadID); // receive thread identifierif( aThread[i] == NULL ){printf("CreateThread error: %d\n", GetLastError());return 1;}}// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);// Close thread and mutex handlesfor( i=0; i < THREADCOUNT; i++ )CloseHandle(aThread[i]);CloseHandle(ghMutex);return 0;
}DWORD WINAPI WriteToDatabase( LPVOID lpParam )
{ // lpParam not used in this example
    UNREFERENCED_PARAMETER(lpParam);DWORD dwCount=0, dwWaitResult; // Request ownership of mutex.while( dwCount < 20 ){ dwWaitResult = WaitForSingleObject( ghMutex,    // handle to mutexINFINITE);  // no time-out intervalswitch (dwWaitResult) {// The thread got ownership of the mutexcase WAIT_OBJECT_0: __try { // TODO: Write to the databaseprintf("Thread %d writing to database...\n", GetCurrentThreadId());dwCount++;} __finally { // Release ownership of the mutex objectif (! ReleaseMutex(ghMutex)) { // Handle error.
                    } } break; // The thread got ownership of an abandoned mutex// The database is in an indeterminate statecase WAIT_ABANDONED: return FALSE; }}return TRUE;
}

Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 736 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
Thread 2504 writing to database...
请按任意键继续. . .

dwCount 为 20,每个线程执行20次。

转载于:https://www.cnblogs.com/lihaozy/archive/2012/06/13/2548465.html

MSDN上的Mutex代码及其执行结果相关推荐

  1. python编写代码求圆的面积_【Python】求圆的面积,书上的代码可执行却是0,不知道为什么...

    首页 专栏 python 文章详情 0 求圆的面积,书上的代码可执行却是0,不知道为什么 唐代芙发布于 今天 01:27 include define PI 3.14159 double Area(d ...

  2. 【青少年编程】【答疑】控制Scratch异步代码的执行顺序

    问题 几天前,我写了一篇图文 对「等待(0)秒」的理解,发现可以利用「等待(0)秒」这个积木块来解决Scratch中异步代码的执行顺序问题,即点击绿旗后可以控制多个角色中响应该事件的代码的顺序. 在这 ...

  3. PHP文件上传主要代码讲解

    导读:在php开发过程中,文件上传也经常用到,这里简单介绍下. 在php开发过程中,文件上传也经常用到,这里简单介绍下. 代码如下: <?php   if($_FILES['myfile'][' ...

  4. php获取svn文件,然后ftp上传服务器代码

    配置文件 <?php //setup commandline $svn_cmd1 = 'svn.exe update '; $svn_cmd2 = ' --username xxx --pass ...

  5. 当try、catch中有return时,finally中的代码会执行么?

    今天,看到一个面试题: try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗? 我们用代码来验证下: public static void mai ...

  6. 浅谈代码的执行效率(4):汇编优化

    终于谈到这个话题了,首先声明我不是汇编优化的高手,甚至于我知道的所有关于汇编优化的内容,仅仅来自于学校的课程.书本及当年做过的一些简单练习.换句话说,我了解的东西只能算是一些原则,甚至也有一些&quo ...

  7. 在Windows上同步SVN代码库到备份SVN机器上

    在Windows上同步SVN代码库到备份SVN机器上 SVN源库IP: 192.168.0.200   SVN目标库(备份库)IP:192.168.0.62   visualSVN Server版本: ...

  8. 浅谈代码的执行效率(3):缓存与局部性

    在前两篇文章里,我们讨论了程序性能的两个方面,一是算法(广义的算法,即解决问题的方法),二是编译器.通过这两个方面,我想表达的意思是,一段程序的执行效率,是很难从表面现象得出结论的,至少从一些简单的层 ...

  9. 浅谈代码的执行效率(2):编译器的威力

    在上一篇文章中,我主要表达了这样一个观点:影响程序效率的关键之一是算法,而算法的选择与优化,和是否多一个赋值少一个判断的关系不大.关于算法的选择,我谈到其理论上的复杂度,并不直接反映出效率.因为在实际 ...

最新文章

  1. Java 门面模式 浅析
  2. Screen OS 6.0 学习笔记一
  3. 2021-03-04 Halcon初学者知识 【18】谈谈秩滤波(Rank filter)
  4. SQL 2005完全卸载,重新安装
  5. kafka发送及消费消息示例
  6. c++STL算法基础
  7. 前端学习(603):计算机基础
  8. navicat导入CSV/Excel文件
  9. 浏览器展示CSS伪类的动画和过渡效果应用
  10. 又回来了~工作告一段落了,终于有时间看书写点东西了
  11. 初窥Javascript单元测试,附带掌握一门新技能的学习方式。
  12. html页面纵向自适应,页面布局之上下固定中间自适应.html
  13. CCF CSP 201512-02 消除类游戏
  14. m7405d粉盒清零方法_联想各种打印机多功能一体机硒鼓清零方法汇总
  15. NSSA区域和Totally NSSA区域
  16. CTF--Do you like xml
  17. 微信公众号 永久图文素材 content html sample
  18. ubuntu20.04截图快捷键
  19. matlab节点连通率,利用MATLAB仿真节点个数和节点通信半径与网络连通率的关系
  20. winrar去掉烦人的广告 亲测有效

热门文章

  1. CVPR 2021 | 超越卷积的自注意力模型,谷歌、UC伯克利提出HaloNet
  2. ECCV 2020 | 微软亚洲研究院精选论文摘录
  3. PyTorch实现的李沐《动手学深度学习》,登上GitHub热榜,获得1000+星
  4. 连续七天熬夜3D建模师终于出手,让老板增加薪资待遇,分享使用3D建模软件的6个行业
  5. 内推 | 字节跳动算法提前批
  6. 【OpenCV】OpenCV函数精讲之 -- 教你如何使用离散傅里叶变换
  7. EAST算法超详细源码解析:数据预处理与标签生成
  8. 深度学习(六十)网络压缩简单总结
  9. c++语言int最大值,c++ 关于如何获取int型的最大值
  10. 如何打造园本特色_如何打造一个可持续发展的特色观光园?