爱易网
IT新闻
IT新闻
爱易资讯
网站搭建
云虚拟主机教程
云服务器教程
Apache教程
IIS教程
Nginx教程
网站策划
站长文章
推广教程
淘宝客教程
网页设计
HTML教程
XHTML教程
CSS教程
HTML5教程
CSS3教程
JavaSript基础
JQuery教程
Node.js教程
前端技术
Ajax教程
Js特效
Xml教程
平面设计
页面UI设计
photoshop教程
程序开发
AI人工智能
Asp教程
Php教程
Asp.Net教程
Net Core教程
C#教程
Java教程
Jsp教程
开发技术
微信小程序教程
Uniapp开发教程
微信公众号开发
Andriod教程
IOS教程
DOS教程
Python教程
Docker教程
Windows Container教程
数据库
MSSQL教程
MySQL教程
Redis教程
Access教程
Oracle教程
数据库教程
操作系统
Linux教程
Windows教程
MAC教程
Cisco教程
交换机教程
防火墙教程
搜索
爱易网页
Windows教程
使用event与无锁算法实现windows平台上的critical section
使用event与无锁算法实现windows平台上的critical section
日期:2014-05-17 浏览次数:21182 次
使用event与无锁算法实现windows平台下的critical section
//#include "own_cs.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <assert.h> #include <malloc.h> #include <stdlib.h> #include <stdio.h> struct own_critical_section { long own_lock_count; // count of locked thread, -1 means unlock , other means lock. HANDLE own_event; // auto-reset DWORD own_owning_thread_id; // owner thread of lock long own_recursion_count; // count of recursion }; void InitialOwnCriticalSection( own_critical_section** own_cs) { *own_cs = (own_critical_section*)malloc( sizeof( own_critical_section ) ); (*own_cs)->own_lock_count = -1; (*own_cs)->own_event = CreateEventW( NULL, FALSE, FALSE, NULL ); (*own_cs)->own_owning_thread_id = 0; (*own_cs)->own_recursion_count = 0; } void DeleteOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs != NULL ); CloseHandle( own_cs->own_event ); free( own_cs ); } void EnterOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); for ( int spin_count = 0; spin_count < 500; ++ spin_count ) {//spinlock if ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, -1L, -1L ) ) break; Sleep(0); } DWORD current_thread_id = ::GetCurrentThreadId(); if( 0 < InterlockedIncrement( &own_cs->own_lock_count ) && ( own_cs->own_owning_thread_id != current_thread_id ) ) { //locked WaitForSingleObject( own_cs->own_event, INFINITE ); } own_cs->own_owning_thread_id = current_thread_id; ++own_cs->own_recursion_count; } void LeaveOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); if ( --own_cs->own_recursion_count == 0 ) own_cs->own_owning_thread_id = 0; if( -1L != InterlockedDecrement( &own_cs->own_lock_count ) && (::GetCurrentThreadId() == own_cs->own_owning_thread_id ) ) { SetEvent( own_cs->own_event ); } } bool TryEnterOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); DWORD current_thread_id = GetCurrentThreadId(); if ( current_thread_id == own_cs->own_owning_thread_id ) { // inside the lock InterlockedIncrement( &own_cs->own_lock_count ); return true; } else{ if ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, 0, -1L ) ) return false; own_cs->own_owning_thread_id = current_thread_id; } ++own_cs->own_recursion_count; return true; }
上一篇:安装更新时出现一些问题,但我们稍后会重试。 错误代码:0x8e5e0408
下一篇: WindowsXP圆满搭建POCO开发环境
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
C#里将windows讯息组成的一个枚举常量
网站内网会访问,外网访问不了
使用COPY命令想将本地的一个文件拷贝到局域网主机上的共享文件夹下,提示Access is denied,该如何解决
请问一个网管题
哪位高手知道有在线破解QQ密码的软件
【升星散分5】是寂寞~是HAPPY~该如何处理
2003系统要登陆才能运行“开机启动”的程序,怎么实现开机未登陆时,就系统运行了那些开机启动的程序
新时尚Windows8开发(40):StreamSocket的应用
为啥远程登录时还显示用户名
推荐阅读
更多>
能上qq,打不开网页,各位朋友都过来帮忙看看,多谢
Memcached windows 上安装与应用
仿WindowsXP画图板设计(一)
Windows 7 专业版的,怎么配置FTP
win 7 旗舰 权限有关问题
XP开机锁桌面,该怎么解决
xp进安全模式就蓝屏,该怎么处理
windows xp 资料默认查看方式的设定
陷入trap的通俗解释,该如何处理
请问访问共享有关问题
Windows不能从此盘读取。此盘可能已损坏,或者使用的格式与Windows不兼容,该怎么处理
网卡丢包解决思路
怎么将影视文件刻录到DVD光盘中(能在碟机上观看的那种)
远路唤醒
windows驱动程序安装文件INF文件疑问,该如何解决
安装Realtek Hd Audio Driver 异常
《coredump有关问题原理探究》windows版8.4节堆布局堆块遍历的第二种方法
怎么以非系统管理员权限运行win7来识别环境变量
鼠标不动的有关问题
装了redhat ubantu没了解决思路