windows TryEnterCriticalSection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6829592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:36:46  来源:igfitidea点击:

TryEnterCriticalSection

c++windowscritical-section

提问by Alecs

I'm not sure if I correctly understand. TryEnterCriticalSectionis called only once, it's not stick like EnterCriticalSection? E.g. if I write something like

我不确定我是否正确理解。TryEnterCriticalSection只被调用一次,它不是像EnterCriticalSection吗?例如,如果我写一些类似的东西

if(TryEnterCriticalSection (&cs))
{
//do something that must be synh
LeaveCriticalSection(&cs);
}
else
{
//do other job
}
//go on

and if TryEnterCriticalSectionreturns false the part do something that must be synhwill never be done, and do other jobpart will be exucuted and then go on?

如果TryEnterCriticalSection返回 false,则部分do something that must be synh将永远不会完成,do other job部分将被执行,然后go on

采纳答案by CharlesB

You guessed right. TryEnterCriticalSection()is called once, and tries to enter the critical section only once. If the critical section is locked, it returns false after the check.

你猜对了。TryEnterCriticalSection()被调用一次,并且只尝试进入临界区一次。如果临界区被锁定,检查后返回false。

In general, if a function returns a boolean or an int, the if/else clauses behaves like following:

通常,如果函数返回布尔值或整数,则 if/else 子句的行为如下:

if (function()) //function() is called once here, return result is checked
{
  //executed if function() returned true or non-zero
}
else
{
  //executed if function() returned false or zero
}
//executed whatever happens

回答by sharptooth

TryEnterCriticalSection()does the following:

TryEnterCriticalSection()执行以下操作:

  • tries to enter a critical section
  • if that section is currently grabbed by some other thread the section is not entered and the function returns zero, otherwise
  • the section is entered and the function returns nonzero
  • 试图进入临界区
  • 如果该部分当前被某个其他线程抓取,则不会进入该部分并且函数返回零,否则
  • 该部分被输入并且函数返回非零

Anyway the function never blocks. Compare it with EnterCriticalSection()that falls through if no other thread has the critical section entered and blocks if such other thread exists.

无论如何,该功能永远不会阻塞。EnterCriticalSection()如果没有其他线程进入临界区,则将其与失败进行比较,如果存在此类其他线程则阻塞。

So the outcome of your code will depend on whether the critical section is entered by another thread at the moment the function is called. Don't forget to call LeaveCriticalSection()for each time when TryEnterCriticalSection()returns nonzero (succeeds).

所以你的代码的结果将取决于临界区是否在调用函数的那一刻被另一个线程进入。LeaveCriticalSection()每次TryEnterCriticalSection()返回非零(成功)时不要忘记调用。

So yes, your code is written with right assumptions.

所以是的,你的代码是用正确的假设编写的。

回答by Abubakar Cool

If your critical section is too small, than you must try this TryEnterCriticalSectionin a loop because it will then acquire less clock cycles to enter critical section and leave that critical section.

如果您的临界区太小,那么您必须TryEnterCriticalSection在循环中尝试此操作,因为它将获得更少的时钟周期以进入临界区并离开该临界区。

回答by Naszta

Yes, your code is correct.

是的,你的代码是正确的。

See more on MSDN.

MSDN上查看更多信息。