.NET Core 3.1 Linux环境下Thread.Priority属性不生效问题咨询
我在Linux环境编译运行.NET Core 3.1程序时,遇到了Thread.Priority设置不生效的问题。经查dotnet runtime源码中已经存在将Thread.Priority转换为POSIX标准优先级的实现,但实际测试依然无法正常生效。为复现问题,我修改了Thread.Priority官方文档中的示例代码,增加了线程数以适配多核CPU场景。
Windows(win-x64)环境运行输出
不同优先级线程的计数差异符合预期:
thread_5 with AboveNormal priority has a count = 563,441,005
thread_15 with Normal priority has a count = 251,310,557
thread_3 with Normal priority has a count = 239,580,270
thread_0 with Normal priority has a count = 236,077,285
thread_2 with AboveNormal priority has a count = 537,902,014
thread_4 with BelowNormal priority has a count = 5,903,969
thread_7 with BelowNormal priority has a count = 5,958,548
thread_8 with AboveNormal priority has a count = 555,518,195
thread_11 with AboveNormal priority has a count = 549,665,909
thread_12 with Normal priority has a count = 258,493,564
thread_14 with AboveNormal priority has a count = 550,025,919
thread_9 with Normal priority has a count = 135,923,866
thread_6 with Normal priority has a count = 138,882,526
thread_13 with BelowNormal priority has a count = 4,273,232
thread_1 with BelowNormal priority has a count = 4,608,543
thread_10 with BelowNormal priority has a count = 5,908,285
Ubuntu(linux-x64)环境运行输出
不同优先级线程的计数无明显差异,看起来所有线程均运行在相同优先级:
thread_4 with BelowNormal priority has a count = 465,192,119
thread_11 with AboveNormal priority has a count = 558,351,324
thread_9 with Normal priority has a count = 459,901,696
thread_10 with BelowNormal priority has a count = 484,271,783
thread_7 with BelowNormal priority has a count = 519,165,910
thread_14 with AboveNormal priority has a count = 487,836,279
thread_12 with Normal priority has a count = 465,635,561
thread_3 with Normal priority has a count = 450,569,622
thread_5 with AboveNormal priority has a count = 503,227,636
thread_6 with Normal priority has a count = 499,300,694
thread_8 with AboveNormal priority has a count = 515,728,537
thread_1 with BelowNormal priority has a count = 547,473,038
thread_15 with Normal priority has a count = 462,427,461
thread_13 with BelowNormal priority has a count = 526,524,771
thread_2 with AboveNormal priority has a count = 493,184,092
thread_0 with Normal priority has a count = 465,902,186
我已经通过setcap命令为程序授予了所需权限:sudo setcap 'cap_sys_nice=eip' ./ThreadPriorityTest
但操作后问题仍未解决,请问还遗漏了哪些配置或操作?
修改后的测试代码
using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using Timers = System.Timers; namespace ThreadPriorityTest { class Test { static void Main() { PriorityTest priorityTest = new PriorityTest(); int threadCount = 16; List<Thread> threads = new List<Thread>(); for(int i=0; i<threadCount; i++) { Thread thread = new Thread(priorityTest.ThreadMethod); thread.Name = $"thread_{i}"; switch(i%3) { case 0: thread.Priority = ThreadPriority.Normal; break; case 1: thread.Priority = ThreadPriority.BelowNormal; break; case 2: thread.Priority = ThreadPriority.AboveNormal; break; } threads.Add(thread); } Thread.CurrentThread.Priority = ThreadPriority.Highest; Console.WriteLine($"Starting {threadCount} threads..."); foreach(Thread thread in threads) { thread.Start(); } Console.WriteLine($"Done starting {threadCount} threads."); priorityTest.StartEvent.Set(); // 允许计数10秒 Thread.Sleep(10000); priorityTest.LoopSwitch = false; } } class PriorityTest { static volatile bool loopSwitch; static ManualResetEvent startEvent = new ManualResetEvent(false); [ThreadStatic] static long threadCount = 0; public PriorityTest() { loopSwitch = true; } public bool LoopSwitch { set { loopSwitch = value; } } public ManualResetEvent StartEvent { get { return startEvent; } } public void ThreadMethod() { startEvent.WaitOne(); while (loopSwitch) { threadCount++; } Console.WriteLine("{0,-11} with {1,11} priority " + "has a count = {2,13}", Thread.CurrentThread.Name, Thread.CurrentThread.Priority.ToString(), threadCount.ToString("N0")); } } }
内容的提问来源于stack exchange,提问作者Pacman

