# Thread#priority= does not work

**URL:** <https://www.ruby-forum.com/t/thread-priority-does-not-work/126109>\
**Category:** Development\
**Created:** [January 15, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/thread-priority-does-not-work/126109 "2008-01-15T13:56:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 15, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/thread-priority-does-not-work/126109/1 "2008-01-15T13:56:44Z")

</div>

e$B1sF#$H?=$7$^$9!#e(B

Thread#priority= e$B$K$h$kM%@hEY$N@\_Dj$,$G$-$J$$$h$&$G$9!#e(B  
e$B0J2\<$Oe(B Thread#priority=  
e$B$N%3%a%s%H$K=q$$$F$"$k%5%s%W%k%3!\<%I$G$9$,!"e(B  
e$B%3%a%s%H$NDL$j$KF0$-$^$;$s!#e(B

$ ./ruby -ve ’  
count1 = count2 = 0

a = Thread.new do  
loop { count1 += 1 }  
end  
a.priority = -1

b = Thread.new do  
loop { count2 += 1 }  
end  
b.priority = -2

sleep 1  
p count1 #=\> 622504  
p count2 #=\> 5832  
’  
ruby 1.9.0 (2008-01-15 revision 0) [i686-linux]  
3196560  
3359563

Thread#priority= e$B$N\<B8=$Ke(B pthread  
e$B$NM%@hEY$r;H$&@oN,$N$h$&$G$9$,!“e(B  
e$B$3$l$K$OLdBj$,$”$k$H;W$$$^$9!#e(B

- pthread e$B$N%G%U%)%k%H$N%9%1%8%e!\<%j%s%0%"%k%4%j%:%`e(B (SCHED\_OTHER)  
e$B$Oe(B  
e$BM%@hEY$rL5;k$9$ke(B
- e$BM%@hEY$r9MN8$7$F$/$l$k%9%1%8%e!\<%j%s%0e(B (SCHED\_RR e$B$He(B  
SCHED\_FIFO) e$B$Oe(B  
e$B%9!\<%Q!\<%P%$%68"8B$,$J$$$H;H$($J$$$i$7$$e(B (e$B\>/$J$/$H$be(B LInux  
e$B$G$Oe(B)

e$B$3$N$h$&$J@)Ls$,$"$k0J\>e!"e(Bpthread  
e$B$NM%@hEY$r;H$&$N$OFq$7$$$H;W$$$^$9!#e(B

e$B2r7hJ}K!$re(B 2 e$B$DDs0F$7$^$9!#e(B

(1) Thread#priority= e$B$re(B deprecated e$B$9$ke(B  
(2) native\_thread\_yield e$B$9$k$H$-$Oe(B priority  
e$B$N?t$@$17+$jJV$7$Fe(B  
e$B5\<;wE\*$KM%@hEY$r\<B8=$9$ke(B

e$B0J2\<$Oe(B (2) e$B$N\<BAu%$%a!\<%8$G$9!#e(Bthread\_win32  
e$B$O$H$j$"$($:L5;k$7$F$^$9!#e(B

# Index: thread.c

— thread.c (revision 15066)  
+++ thread.c (working copy)  
@@ -676,13 +676,16 @@  
thread\_debug(“rb\_thread\_schedule\n”);  
if (!rb\_thread\_alone()) {  
rb\_thread\_t \*th = GET\_THREAD();

- int priority = th-\>priority;

- 

```
 native_thread_yield();

```

- 

```
 while(priority++ < 0) {

```

- native\_thread\_yield();
- 

```
 }

```

}  
native\_mutex\_lock(&th-\>vm-\>global\_interpreter\_lock);

e$B$D$$$G$G$9$,!"2\>$Ke(B pthread  
e$B$NM%@hEY$,$&$^$/;H$($?$H$7$F$b!"8=:\_$Ne(B  
native\_thread\_apply\_priority e$B$Oe(B min/max  
e$B$N%A%’%C%/$,$J$s$+JQ$Je(B  
e$B5$$,$7$^$9!#e(B

# Index: thread\_pthread.c

— thread\_pthread.c (revision 15066)  
+++ thread\_pthread.c (working copy)  
@@ -355,12 +355,12 @@  
max = sched\_get\_priority\_max(policy);  
min = sched\_get\_priority\_min(policy);

- if (min \< priority) {

- if (min \> priority) {
- priority = min;
- }
- else if (max \< priority) {  
priority = max;  
}

- else if (max \> priority) {

- priority = min;

- }

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [May 19, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/thread-priority-does-not-work/126109/2 "2008-05-19T12:08:02Z")

</div>

e$B!!$5$5$@$G$9!%e(B

e$B!!e(BIRC  
e$B$G8@$o$l$F5$$E$$$?$s$G$9$,!$$3$N7o$r$9$C$+$j8+F($7$F$^$7$?!%e(B

Yusuke ENDOH wrote:

> e$B2r7hJ}K!$re(B 2 e$B$DDs0F$7$^$9!#e(B
> 
> (1) Thread#priority= e$B$re(B deprecated e$B$9$ke(B  
> (2) native\_thread\_yield e$B$9$k$H$-$Oe(B priority e$B$N?t$@$17+$jJV$7$Fe(B  
> e$B5\<;wE\*$KM%@hEY$r\<B8=$9$ke(B

e$B!!e(BThread#priority  
e$B$r0U\<1$7$F%W%m%0%i%\_%s%0$r$9$k$H$$$&$N$O$I$NDxEYe(B  
Ruby e$B$G0lHLE\*$J$s$G$7$g$&$+!%$=$l$K$h$C$F!$e(B(1), (2),  
e$B$b$7$/$Oe(B (3)  
Native Thread e$B$N\<BAu$K$\*G$$;!$$,$"$k$N$G$O$J$$$+$H;W$$$^$9!%e(BYARV  
e$B$Ge(B  
e$B$Oe(B (3) e$B$G$$$C$+!\<!$$H;W$C$F$$$^$7$?!%e(B

e$B!!$=$l$O$=$l$H$7$F!$D:$$$?%Q%C%A!J8e\<T!K$OE,MQ$7$F$\*$-$^$9!%e(B

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [May 20, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/thread-priority-does-not-work/126109/3 "2008-05-20T15:22:03Z")

</div>

e$B1sF#$G$9!#e(B

08/05/19 e$B$Ke(B SASADA Koichi[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) e$B$5$s$O=q$-$^$7$?e(B:

> IRC e$B$G8@$o$l$F5$$E$$$?$s$G$9$,!$$3$N7o$r$9$C$+$j8+F($7$F$^$7$?!%e(B

e$B;d$bK:$l$F$^$7$?!#e(B

> Yusuke ENDOH wrote:  
> e$B!!e(BThread#priority e$B$r0U\<1$7$F%W%m%0%i%\_%s%0$r$9$k$H$$$&$N$O$I$NDxEYe(B  
> Ruby e$B$G0lHLE\*$J$s$G$7$g$&$+!%e(B

e$B$I$&$J$s$G$7$g$&!#;d$O;H$C$?$3$H$,$“$j$^$;$s$,!”%G%P%C%0$de(B  
e$B%W%m%U%!%$%k$K$O;H$($k$+$J$!!"$H;W$o$J$/$b$J$$$G$9!#e(B  
e$B%?%$%_%s%0$r$:$i$7$F$_$k$H$+!"7WB,%9%l%C%I$rCY$a$k$H$+!#e(B

> e$B$=$l$K$h$C$F!$e(B(1), (2), e$B$b$7$/$Oe(B (3)  
> Native Thread e$B$N\<BAu$K$\*G$$;!$$,$"$k$N$G$O$J$$$+$H;W$$$^$9!%e(BYARV e$B$Ge(B  
> e$B$Oe(B (3) e$B$G$$$C$+!\<!$$H;W$C$F$$$^$7$?!%e(B

e$B4D6-$K$h$C$F$OL5;k$5$l$k$C$F$3$H$G$9$M!#e(B  
e$B%I%-%e%a%s%H$K$R$H$3$HIU$12C$($l$P$=$l$b$$$$$H;W$$$^$9!#e(B

# Index: thread.c

— thread.c (revision 16492)  
+++ thread.c (working copy)  
@@ -1641,7 +1641,7 @@

- Returns the priority of _thr_. Default is inherited from the
- current thread which creating the new thread, or zero for the
- initial main thread; higher-priority threads will run before

- 
  - lower-priority threads.

- 
  - lower-priority threads. The priority may be ignored on some  
platforms.
  - 
  - 

```
Thread.current.priority #=> 0

```

\*/  
@@ -1660,7 +1660,8 @@
  - 

```
thr.priority= integer => thr

```

  - 
  - Sets the priority of _thr_ to _integer_. Higher-priority  
threads

- 
  - will run before lower-priority threads.

- 
  - will run before lower-priority threads. The priority may be  
ignored on

- 
  - some platforms.
  - 
  - 

```
count1 = count2 = 0

```

  - 

```
a = Thread.new do

```
