TryEnterCriticalSection

本文探讨了TryEnterCriticalSection函数的应用场景及其实现原理。不同于EnterCriticalSection会导致线程阻塞,TryEnterCriticalSection允许线程检查资源是否可用,若不可用则直接返回,避免了不必要的等待。

EnterCriticalSection,如果遇到还没释放的,线程会阻塞住。

 

 if ( TryEnterCriticalSection(&m_cs_XX) != 0 )
 {
       //  do something

       LeaveCriticalSection(&m_cs_XX);

}

 

 如果EnterCriticalSection将一个线程置于等待状态,那么该线程在很长时间内就不能再次被调度。实际上,在编写得不好的应用程序中,该线程永远不会再次被赋予CPU时间。TryEnterCriticalSection函数决不允许调用线程进入等待状态。它的返回值能够指明调用线程是否能够获得对资源的访问权。TryEnterCriticalSection发现该资源已经被另一个线程访问,它就返回FALSE。在其他所有情况下,它均返回TRUE。运用这个函数,线程能够迅速查看它是否可以访问某个共享资源,如果不能访问,那么它可以继续执行某些其他操作,而不必进行等待。如果TryEnterCriticalSection函数确实返回了TRUE,那么CRITICAL_SECTION的成员变量已经更新。Windows98没有可以使用的TryEnterCriticalSection函数的实现代码。

 

相当同步和异步。

把下面的window代码修改成移植到linux代码#if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _HEADER_BASESYSTEMCONFIG #define _HEADER_BASESYSTEMCONFIG #if _MSC_VER >= 1400 # ifndef _CRT_SECURE_NO_DEPRECATE # define _CRT_SECURE_NO_DEPRECATE # endif // [11/8/2006 Tgame.Tang] //# ifndef _DEBUG //# ifndef _SECURE_SCL //# define _SECURE_SCL 0//在release下关闭STL的安全特性,提高效率 //# else //# if _SECURE_SCL != 0 //# error("_SECURE_SCL must be defined as 0 while compile no debug version,You must include stl after include this file") //# endif //# endif //# endif #endif #pragma warning(disable:4995) #pragma warning (disable:4251) //!系统平台 #if defined(_WIN32) // // Comment out the following block if you want to run on Windows 9x // or Windows NT 3.51. // # ifndef _WIN32_WINNT // // Necessary for TryEnterCriticalSection. // # define _WIN32_WINNT 0x0500 # endif #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 # include <windows.h> # include <winsock2.h> # pragma comment( lib, "wsock32.lib" ) # pragma comment( lib, "Ws2_32.lib" ) #elif (defined(__sun) && defined(__sparc)) || (defined(__hpux)) # include <inttypes.h> #else // // The ISO C99 standard specifies that in C++ implementations the // macros for minimum/maximum integer values should only be defined if // explicitly requested with __STDC_LIMIT_MACROS. // # ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS # endif # include <stdint.h> #endif //!字节序 #if defined(__i386) || defined(_M_IX86) || defined (__x86_64) # define SERIAL_LITTLE_ENDIAN #elif defined(__sparc) || defined(__sparc__) || defined(__hppa) || defined(__ppc__) || defined(_ARCH_COM) # define SERIAL_BIG_ENDIAN #else # error "Unknown architecture" #endif //!STL #include <vector> #include <string> #include <cassert> #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <stack> #include <list> #ifdef _Foundation_Import_ # define _Foundation_Export_ __declspec( dllimport ) #else # define _Foundation_Export_ __declspec( dllexport ) //# pragma warning (disable:4251) #endif //#define _Disable_Vt_ #endif
最新发布
09-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值