Thread#priority= does not work

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

Thread#priority= e$B$K$h$kM%@[email protected]_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$&[email protected])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
[email protected]$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;

    thread_debug(“rb_thread_schedule/switch start\n”);

    rb_gc_save_machine_context(th);
    native_mutex_unlock(&th->vm->global_interpreter_lock);
    {

  •  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;

  • }

    sp.sched_priority = priority;
    pthread_setschedparam(th->thread_id, policy, &sp);

[email protected]$G$9!%e(B

e$B!!e(BIRC
[email protected]$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 [email protected]$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

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

08/05/19 e$B$Ke(B SASADA Koichi[email protected] e$B$5$s$O=q$-$^$7$?e(B:

IRC [email protected]$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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs