goto 会违反互斥体吗?
我做错了,是吗?
...
if( you_think_youre_genius )
goto goto_sucks:
...
pthread_mutex_lock(&mutex);
do_stuff();
goto_sucks:
do_other_stuff();
pthread_mutex_unlock(&mutex);
I am doing it wrong, yes?
...
if( you_think_youre_genius )
goto goto_sucks:
...
pthread_mutex_lock(&mutex);
do_stuff();
goto_sucks:
do_other_stuff();
pthread_mutex_unlock(&mutex);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,goto 是在二进制代码级别直接跳转,因此 goto 和标签之间的任何函数调用都将被跳过。
Yes, goto is direct jmp down at the binary code level so any function calls between the goto and the label will be skipped, period.
互斥体是在 pthread_mutex_lock 函数内部获取的。如果您跳过函数调用,您将不会获得互斥锁。如果您尝试两次锁定互斥锁,则可能会出现死锁。如果您尝试解锁不属于您的互斥体,则可能会严重破坏事情。
The mutex is acquired inside the
pthread_mutex_lock
function. If you jump past the function call, you will not have acquired the mutex. If you try to lock a mutex twice, you may deadlock. If you try to unlock a mutex you do not own, you may break things very badly.如果条件为真,则将在不锁定互斥锁的情况下调用
do_other_stuff
,然后释放互斥锁而不锁定它。 完全错误!只是没有 goto
If the condition is true,
do_other_stuff
will be called without the mutex being locked, and then the mutex will be released without locking it. Plain wrong!Just without goto