线程持有自旋锁的疑问 [英] Thread holding spinlock doubt

查看:98
本文介绍了线程持有自旋锁的疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持有自旋锁删除线程安全吗?
我的意思是,如果说一个线程t1正在持有一个自旋锁,那么如果说t1没有释放该自旋锁,那么另一个想要该自旋锁的线程t2可以删除线程t1吗?

Are thread holding spinlocks deletion safe?
I mean if say a thread t1 is holding a spinlock, then can an another thread t2 wanting the spinlock delete the thread t1 if say t1 is not releasing the spinlock?

推荐答案

在删除线程t1之后,该锁仍将处于未签名状态.如果终止t1,则仍然需要释放锁.
After deleting thread t1, the lock will still be in a non-signalled state. If you terminate t1, then you still need to release the lock.


小心该斧头Eugene(对于斧头,请阅读终止线程"). 自旋锁"(或与此相关的任何锁)的常见用途之一是确保对象和/或结构处于一致状态并在原子操作"中进行修改.例如,链接列表中的前进/后退和头/尾链接.

因此,如果您决定启动并杀死拥有自旋锁的线程,则不仅该锁处于不确定状态"(可能是锁定"状态),而且它所操作的任何对象/结构也都处于不确定状态".在不确定状态下,即某些项目可能会更新,而其他项目可能不会更新.这意味着该对象可能会在以后的更新中变得无用,它不是一致的".

因此,从杀死线程中恢复可能比重置自旋锁要复杂得多.
Careful with that axe, Eugene (for axe, read "terminate thread"). One of the common uses for "spin locks" (or any lock for that matter) is to ensure that an object and/or structure is in a consistent state and is modified in an "atomic operation". For example, forward / backward and head / tail links in a linked list.

So, if you decide to up and kill a thread that owns a spin lock, not only is the lock in an "indeterminate state" (chances are it''s "locked") but any object / structure it was operating on is also in an indeterminate state, that is, some of the items may be updated, others may not. This means that the object may be rendered useless for future updates, it is not "consistent".

So your recovery from killing the thread might be a lot more complex that just resetting the spin lock.


这篇关于线程持有自旋锁的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆