简体   繁体   English

等待互斥量的线程如何重新运行?

[英]How is a thread waiting for mutex put back to running?

The context is like this: 上下文是这样的:

  • a thread tries to lock a already locked mutex 线程试图锁定已经锁定的互斥锁
  • the thread is put to sleep/blocking 线程进入睡眠/阻塞状态
  • after some while, the mutex is unlocked 一段时间后,互斥体被解锁

Q1) What will happen then ? Q1)那会发生什么?

will the thread be immediately put back to running? 线程会立即恢复运行吗? Or kernel will still wait the running thread consume its time slice and schedule the waiting thread normally? 还是内核仍然等待正在运行的线程消耗其时间片并正常调度正在等待的线程?

Q2) What if the mutex is not unlocked forever? Q2)如果互斥锁没有永远解锁怎么办? How does the kernel determine to keep the thread waiting? 内核如何确定让线程等待?

Will the thread be immediately put back to running? 线程会立即恢复运行吗? Or kernel will still wait the running thread consume its time slice and schedule the waiting thread normally? 还是内核仍然等待正在运行的线程消耗其时间片并正常调度正在等待的线程?

Typically the thread is now ready-to-run. 通常,该线程现在可以运行了。 On most systems, if there's an available core, it will begin running immediately. 在大多数系统上,如果有可用的内核,它将立即开始运行。 If not, then it will be considered the next time the scheduler is invoked on any core. 如果不是,则下次在任何内核上调用调度程序时,将考虑该问题。

What if the mutex is not unlocked forever? 如果互斥锁没有永远解锁怎么办? How does the kernel determine to keep the thread waiting? 内核如何确定让线程等待?

Typically, the first thing the thread does when it wakes up is try to lock the mutex. 通常,线程在唤醒时要做的第一件事就是尝试锁定互斥锁。 If it fails, it blocks again. 如果失败,它将再次阻塞。 Some implementations assign the mutex to a particular thread before they make it ready-to-run, in which case the thread wakes up with the mutex. 一些实现将互斥锁分配给特定线程之前,它们就可以立即运行,在这种情况下,线程会随着互斥锁唤醒。

Implementations vary and may do anything that conforms to the requirements. 实现方式各不相同,并且可以做任何符合要求的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM