# Thread scheduling

**URL:** <https://www.ruby-forum.com/t/thread-scheduling/52104>\
**Category:** Ruby\
**Created:** [January 10, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104 "2006-01-10T23:53:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 10, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/1 "2006-01-10T23:53:17Z")

</div>

I’ve search for documentation about whether Ruby thread scheduling  
uses FIFO or round-robin and can’t find anything. It seems to be  
round-robin. Is that correct?

Is there a way to control the time slice used for each thread running  
at the same priority?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 10:55am UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/2 "2006-01-11T10:55:34Z")

</div>

Mark V. wrote:

> I’ve search for documentation about whether Ruby thread scheduling  
> uses FIFO or round-robin and can’t find anything. It seems to be  
> round-robin. Is that correct?
> 
> Is there a way to control the time slice used for each thread running  
> at the same priority?

No direct means AFAIK. You can always use Thread#pass but I recommend  
to  
not to. Also, there are no thread priorities AFAIK.

```
robert
```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/3 "2006-01-11T15:10:49Z")

</div>

On 1/11/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mark V. wrote:
> 
> > I’ve search for documentation about whether Ruby thread scheduling  
> > uses FIFO or round-robin and can’t find anything. It seems to be  
> > round-robin. Is that correct?
> > 
> > Is there a way to control the time slice used for each thread running  
> > at the same priority?
> 
> No direct means AFAIK. You can always use Thread#pass but I recommend to  
> not to. Also, there are no thread priorities AFAIK.

The Thread class has priority and priority= instance methods. I  
created two threads and noticed that they were time slicing. Then I  
set the priority of one to be higher than the other and it got all the  
time until it completed. So thread priorities seem to be supported.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/4 "2006-01-11T15:48:20Z")

</div>

Mark V. wrote:

> I’ve search for documentation about whether Ruby thread scheduling  
> uses FIFO or round-robin and can’t find anything. It seems to be  
> round-robin. Is that correct?

all threads being equal, it’s round robin.

the thread scheduler is called rb\_thread\_schedule in the eval.c file.

> Is there a way to control the time slice used for each thread running  
> at the same priority?

you would have to change the hard-coded time (integer) values  
in the eval.c file and recompile ruby.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/5 "2006-01-11T16:18:54Z")

</div>

On 1/11/06, Lou V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Is there a way to control the time slice used for each thread running  
> > at the same priority?
> 
> you would have to change the hard-coded time (integer) values  
> in the eval.c file and recompile ruby.

Thanks for the info! That’s some tough code to read. I thought I  
could at least find the time slice duration, but I failed. Do you know  
which variable/constant holds that? Is it WAIT\_TIME? That has a  
value of (1\<\<2) which I guess is equal to 4. 4 what? milliseconds?  
microseconds?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 11, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/thread-scheduling/52104/6 "2006-01-11T17:30:05Z")

</div>

times are set in either function:

thread\_timer, or  
rb\_thread\_start\_timer,

depending on whether \_THREAD\_SAFE is set (on my platform it is).

On my platform, time slice is set to 10 milliseconds:

req.tv\_nsec = 10000000;

WAIT\_TIME is just a flag. It is not the amount of time to wait.
