doudeng1870 2017-11-22 05:08
浏览 246
已采纳

互斥锁锁定和延迟互斥锁解锁顺序

in golang, sync.Mutex Lock and Unlock are usaul operation,but what is the correct order of Lock and defer Unlock?

mu.Lock()
defer mu.Unlock()

or

defer mu.Unlock()
mu.Lock()

which is best?

  • 写回答

3条回答 默认 最新

  • dth20986 2017-11-22 05:10
    关注

    It doesn't matter.

    Either way, defer causes mu.Unlock() to be executed when the current scope is exited (e.g. a function that returns).

    The first method it's preferable, because it has a more natural ordering (lock, then unlock) for human readability.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题