Funcenstein4
Let's take a look at one more termination-handling scenario:
DWORD Funcenstein4() {
DWORD dwTemp;
// 1. Do any processing here.
...
__try {
// 2. Request permission to access
// protected data, and then use it.
WaitForSingleObject(g_hSem, INFINITE);
g_dwProtectedData = 5;
dwTemp = g_dwProtectedData;
// Return the new value.
return(dwTemp);
}
__finally {
// 3. Allow others to use protected data.
ReleaseSemaphore(g_hSem, 1, NULL);
return(103);
}
// Continue processing--this code will never execute.
dwTemp = 9;
return(dwTemp);
}
In Funcenstein4, thetry block will execute and try to return the value ofdwTemp
(5) back toFuncenstein4's caller. As noted in the discussion ofFuncenstein2,
trying to return prematurely from atry block causes the generation of code that puts the return value into a temporary variable created by the compiler. Then the code inside thefinally
block is executed. Notice that in this variation onFuncenstein2 I have added areturn statement to
thefinally block. WillFuncenstein4 return5
or103 to the caller? The answer is103 because thereturn
statement in thefinally block causes the value103 to be stored in the same temporary variable in which
the value5 has been stored, overwriting the5. When thefinally
block completes execution, the value now in the temporary variable (103) is returned fromFuncenstein4
to its caller.
本例中,try部分将返回dwTemp(值为5)。如前所述,在try中的return会导致编译器产生额外的代码用来把返回值放到一个临时变量里,然后去执行finally中的内容。与前面的例子不同的是,本例在finally里增加了一个return,这次会返回5还是103呢?答案是103,因为finally中dereturn导致103保存到了刚才保存5的临时变量。
We've seen termination handlers do an effective job of rescuing execution from a premature exit of thetry block, and we've also seen termination handlers produce
an unwanted result because they prevented a premature exit of thetry block. A good rule of thumb is to avoid any statements thatwould
cause a premature exit of thetry block part of a termination handler. In fact, it is always best to remove allreturns,continues,breaks,gotos,
and so on from inside both thetry andfinally blocks of a termination handler and to put these statements
outside the handler. Such a practice will cause the compiler to generate both a smaller amount of code—because it won't have to catch premature exits from thetry block—and faster
code, because it will have fewer instructions to execute in order to perform the local unwind. In addition, your code will be much easier to read and maintain.
本我们看到了termination hander在try中出现提前退出时产生的后果。一条很好的原则就是,不要在try中制造提前退出。具体地讲,就是在try、finally里不要出现return、continue、break、goto。这样编译器产生的代码会少很多,而且由于不需要执行local unwind,代码执行速度也会快得多。而且,代码也会更易读、维护。
分享到:
相关推荐
Chapter 3: System Programming Concepts Chapter 4: File I/O: The Universal I/O Model Chapter 5: File I/O: Further Details Chapter 6: Processes Chapter 7: Memory Allocation Chapter 8: Users and Groups ...
MOLAND: Termination of the consent solicitation.pdf
Termination of Coverage.pdf
Advanced Process Termination 2.1 用多种方法结束进程 一般用于测试安全工具的自我保护的测试
一份描述网口Bob Smith的电路,对于了解此电路的功能有很大的帮助
Chapter 3: The Application Architecture Hierarchy The Application Framework Generating an Application with AppWizard Understanding the Generated Code CDocument and CView CWinApp CWinThread ...
2015-09-12 07:01:00.692: [ CRSAPP][12074]32CheckResource error for ora.lxhgnc.lxhgnc2.inst error code = -2 2015-09-12 07:01:00.935: [ CRSEVT][12331]32CAAMonitorHandler :: 0:Could not join /opt/oracle/...
Atom-Termination.zip,Integrated terminal for Atom. Looks like terminal-plus, acts like your native terminal (except every other Friday). Looking for collaborators! :-)终止,atom是一个用web技术构建的...
In the past there have been two main termination specifications for UTP data cable infrastructure and yet another for phone cable infrastructure. In today’s world of structured cable systems where ...
一篇关于x264快速自适应模式选择的论文:Fast Adaptive Early Termination for Mode Selection in H.264AVC Standard Based on x264 Implementation
反射弧EC2实例终止保护已禁用 用于检测是否已为EC2实例禁用实例终止保护的Reflex规则。 要了解有关EC2实例终止保护的更多信息,请参阅。 入门 要开始使用Reflex,请查看。 用法 要使用此规则,请将其添加到您的...
该项目确保Kubernetes控制平面通过API或控制台对可能导致EC2实例变得不可用的事件做出适当响应,例如, , , 和EC2实例终止。 如果不加以处理,则您的应用程序代码可能无法正常停止,需要更长的时间才能恢复完全...
使用这个程序可以轻易关闭著名杀毒软件、系统关键进程,但使用前请关闭杀毒软件&防火墙,以免误杀。
ecs-spot-termination配置集 此配置集安装了一个现场终止脚本,该脚本将连续运行并监视现场两分钟警告。 看到两分钟的警告后,它将从ECS群集中耗尽实例。什么是lono配置集? Lono配置集允许通常嵌入在模板中的 可...
An additional 4th port allows proper termination of the antenna when turned off. This SP3T offers low insertion loss and high robustness against interferer signals at the antenna port, while ...
Unix网络编程卷1,第三版,英文版。... Part 3: Advanced Sockets Chapter 12. IPv4 and IPv6 Interoperability Section 12.1. Introduction Section 12.2. IPv4 Client, IPv6 Server Section ...
Transmission lines equations for coupled lines symmetrical lines and symmetrically driven homogeneous medium symmetrical lines in homogeneous medium symmetrical lines and asymmetrical driven ...
《UNIX Network Programming Volume 2》(Unix网络编程卷2英文版,djvu格式,带绿色小巧的阅读器) 原书名: UNIX Network Programming Volume 2:Interprocess Communications 2nd ed. 原出版社: Prentice Hall/...
2 Representing processes in Linux 3 Organising the task structures 4 Wait queues 5 Mutual exclusion with locks 6 Mutual exclusion with waiting 7 Scheduling 8 Process creation 9 Process termination 10 ...