return语句应该在锁的内部还是外部?

编程入门 行业动态 更新时间:2024-10-25 18:35:49
本文介绍了return语句应该在锁的内部还是外部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚意识到,在代码的某些地方,return语句位于锁内部,有时位于外部.哪一个最好?

I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best?

1)

void example() { lock (mutex) { //... } return myData; }

2)

void example() { lock (mutex) { //... return myData; } }

我应该使用哪个?

推荐答案

从本质上讲,无论哪种方法都使代码更简单.单点退出是一个很好的理想选择,但是我不会为了实现它而使代码变形……如果替代方法是声明一个局部变量(在锁之外),对其进行初始化(在锁内),然后然后将其返回(在锁之外),然后我会说,在锁内简单的"return foo"要简单得多.

Essentially, which-ever makes the code simpler. Single point of exit is a nice ideal, but I wouldn't bend the code out of shape just to achieve it... And if the alternative is declaring a local variable (outside the lock), initializing it (inside the lock) and then returning it (outside the lock), then I'd say that a simple "return foo" inside the lock is a lot simpler.

要显示IL的差异,请编写代码:

To show the difference in IL, lets code:

static class Program { static void Main() { } static readonly object sync = new object(); static int GetValue() { return 5; } static int ReturnInside() { lock (sync) { return GetValue(); } } static int ReturnOutside() { int val; lock (sync) { val = GetValue(); } return val; } }

(请注意,我很乐意认为ReturnInside是C#的更简单/更简洁的语言)

(note that I'd happily argue that ReturnInside is a simpler/cleaner bit of C#)

并查看IL(释放模式等):

And look at the IL (release mode etc):

.method private hidebysig static int32 ReturnInside() cil managed { .maxstack 2 .locals init ( [0] int32 CS$1$0000, [1] object CS$2$0001) L_0000: ldsfld object Program::sync L_0005: dup L_0006: stloc.1 L_0007: call void [mscorlib]System.Threading.Monitor::Enter(object) L_000c: call int32 Program::GetValue() L_0011: stloc.0 L_0012: leave.s L_001b L_0014: ldloc.1 L_0015: call void [mscorlib]System.Threading.Monitor::Exit(object) L_001a: endfinally L_001b: ldloc.0 L_001c: ret .try L_000c to L_0014 finally handler L_0014 to L_001b } method private hidebysig static int32 ReturnOutside() cil managed { .maxstack 2 .locals init ( [0] int32 val, [1] object CS$2$0000) L_0000: ldsfld object Program::sync L_0005: dup L_0006: stloc.1 L_0007: call void [mscorlib]System.Threading.Monitor::Enter(object) L_000c: call int32 Program::GetValue() L_0011: stloc.0 L_0012: leave.s L_001b L_0014: ldloc.1 L_0015: call void [mscorlib]System.Threading.Monitor::Exit(object) L_001a: endfinally L_001b: ldloc.0 L_001c: ret .try L_000c to L_0014 finally handler L_0014 to L_001b }

因此,在IL级别,它们[给或取一些名字]是相同的(我学到了一些东西;-p). 因此,唯一明智的比较是本地编码样式的(高度主观的)定律...为简单起见,我更喜欢ReturnInside,但我都不会对此感到兴奋.

So at the IL level they are [give or take some names] identical (I learnt something ;-p). As such, the only sensible comparison is the (highly subjective) law of local coding style... I prefer ReturnInside for simplicity, but I wouldn't get excited about either.

更多推荐

return语句应该在锁的内部还是外部?

本文发布于:2023-10-27 11:43:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!