睡眠0有特殊含义吗? [英] sleep 0 has special meaning?

查看:115
本文介绍了睡眠0有特殊含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个客户项目中,我看到了很多sleep 0的用法.

I'm seeing a lot of usages of sleep 0 in my one of my client project.

代码看起来像这样.

while true 
  ...
  ...
  sleep 0
end

通读诸如 this 之类的一些答案,看来sleep 0具有某些意义

Reading through some of the answer of SO like this it seems that sleep 0 has some significance.

我现在想知道的是,在时间片0期间安排其他线程运行(如果它们正在等待运行)的调度是像ruby或python这样的lang VM的工作,还是内核.

What I want to known now is that does scheduling for other thread to run (if they are waiting to run) during the time slice 0 is a job of a lang VM like ruby or python or it is a job of kernel.

就像上面链接中提到的那样,Ruby VM愿意兑现sleep 0.

In order word does Ruby VM would honor sleep 0 like it is mention in the above link.

推荐答案

是的,出于几个原因,首先,(mri)红宝石线程是带有附加GVL锁的本地线程的包装.

Yes, for a couple of reasons, first of all, (mri) ruby threads are wrappers around native threads with an additional GVL lock.

本质上,当您调用sleep时,ruby所做的就是调用底层的,本地的,依赖于平台的睡眠并释放GVL,以便其他正在运行的线程可以获取它.因此,sleep(0)既屈服于可能正在等待执行的其他本机线程,又释放了当前线程对GVL的保留,否则将阻止Ruby VM执行.

Essentially what ruby is doing when you call sleep is calling the underlying, native, platform dependent sleep and releasing the GVL so that other running threads can acquire it. So sleep(0) is both yielding to other native threads that may be waiting to be executed as well as releasing the current thread's hold on the GVL which would otherwise keep the Ruby VM from executing.

以下是您如何从mri来源看到此内容的简短摘要:

Here's a quick rundown of how you can see this from mri source:

  1. 我们从 https://github获得内核睡眠的定义我们看到的.com/ruby​​/ruby​​/blob/trunk/process.c#L7542 是在c中的函数rb_f_sleep
  2. 中实现的
  3. 接下来,我们转到rb_f_sleep,看到在使用单个参数的情况下,它调用 rb_thread_wait_for
  4. 转到rb_thread_wait_for定义,我们看到对 sleep_timeval
  5. sleep_timeval调用了 native_sleep
  6. native_sleep是平台相关的,分别在posix和Windows系统的thread_pthread.c和thread_win32.c中实现.无论哪种情况,我们都会看到在GVL_UNLOCK_BEGIN 此处的调用此处
  1. We get the definition of Kernel sleep from https://github.com/ruby/ruby/blob/trunk/process.c#L7542, which we see is implemented in c in the function rb_f_sleep
  2. Next we go to rb_f_sleep and see that in the case of a single argument it calls rb_thread_wait_for
  3. Going to the rb_thread_wait_for definition we see a call to sleep_timeval
  4. sleep_timeval has a call to native_sleep
  5. native_sleep is platform dependent and implemented in thread_pthread.c and thread_win32.c for posix and windows systems respectively. In either case we see calls to GVL_UNLOCK_BEGIN here and here

编辑

更准确地说:

Windows:

native_sleep的Windows实现使用WaitForMultipleObjects确实会产生剩余的时间片,请参见:

The windows implementation of native_sleep uses WaitForMultipleObjects which does indeed yield the remaining time slice, see: Does WaitForSingleObject give up a thread's time slice?

Posix:

posix实现使用pthread_cond_timedwait,它会阻塞当前正在运行的线程.

The posix implementation uses pthread_cond_timedwait, which blocks the currently running thread.

无论哪种方式,这里要注意的主要是Ruby线程使用操作系统的底层线程阻塞机制,并通过任何睡眠调用释放GVL,从而允许其他线程进行控制.

Either way, the main thing to note here is that Ruby threads use the underlying thread blocking mechanisms of the OS and release the GVL with any call to sleep, allowing other threads to take control.

这篇关于睡眠0有特殊含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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