爱易网
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 浏览次数:20914 次
使用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; }
上一篇:hyper-v管理器中-虚拟机-连接不上,看不到画面
下一篇: WindowsXP圆满搭建POCO开发环境
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
有哪位高手用过,DISKGEN?分区方面的请求?接分
在IBM刀片服务器上面如何安装windows 2003
配备 Windows 时间服务以使用外部时间源
技术性有关问题
jetty plugin在windows停maven项目静态缓存文件修改
赶在2012元旦事前散分攒人品~3
急求:在win7的控制台(console)中,怎么使得level=asInvoke的程序以管理员权限运行
微软“IT事人人事”(BIEB)的趣味漫画(2)解决方法
双系统 xp 和centos 之间怎么共享文件
推荐阅读
更多>
邮件群发,该如何解决
一个IP地址的子网区划的题目
在WindowsXP中展示隐藏文件
局域网怎么访问另外一个局域网的计算机
为啥现在网络电视如此占网速?解决方案
server 2008 R2 禁止默许C$共享
批处理文件移动,该怎么处理
Windows种标识符
win7家庭版晋级为Win7旗舰版;超级简单方法
操作系统中修改BIOS设置软件,该怎么处理
IE或explorer有关问题(不停地打开“小弟我的文档”文件夹)
dos无盘工作站上用net use命令连接win2003 server报错
在visio中安插表格
PStoreCreateInstance能不能获取用ie7登录的账号信息解决方法
请问: Win2003服务器,装什么杀毒软件呢? 要免费的,呵呵
不是说hosts比DNS级别高么解决方案
Windows不能在本地计算机启动iis admin 异常代码-2146893818
怎么让U盘自动运行提醒对话框
guest来宾用户删除不了 如何会(有图)
小弟我的机器中毒,请各位帮帮忙,多谢啊