什么时候发生?处于关键部分时线程被挂起 [英] When does this happen? Thread suspended while in critical section

查看:309
本文介绍了什么时候发生?处于关键部分时线程被挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,如果某个线程位于关键部分,可以抢占它吗?

I'm just wondering, if a thread is in a critical section, can it be preempted?

  • 线程A :输入CR
  • 线程A :被暂停
  • 线程B :想要输入CR但不能输入,因为线程A 具有锁
  • Thread A: Enter CR
  • Thread A: Get suspended
  • Thread B: Wants to enter CR but can't, because Thread A has the lock

如果线程A 被抢占,因此互斥锁被线程A 卡住了,该怎么办?

If Thread A preempted, and so the mutex lock is stuck with Thread A, what can be done about this?

推荐答案

当然可以抢占它.否则,如果允许在进程内运行的唯一线程是拥有关键部分的线程,其他线程将如何尝试进入该关键部分?

Of course it can be preempted. Otherwise how the other threads could try to enter that critical section, if the only thread that is allowed to run within the process is the thread that owns the critical section?

示例中的线程B将等待,直到重新计划线程A并完成关键部分为止.毫不奇怪.而且,如果线程A在关键部分同时也等待着主题B拥有的互斥锁,那么这是一个死锁,您必须通过修改逻辑来解决.

Thread B in your example will wait until thread A is rescheduled and is finished with the crtical section. No surprise here. And if thread A, while in a critical section, also waits for a mutex owned by thead B, then it's a deadlock which you must resolve by revising your logic.

这篇关于什么时候发生?处于关键部分时线程被挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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