Bitcointalk · bitcoin auto-renice-ing

中本聪,2010 年 3 月 15 日

SN-0433 已核对来源,附原文与上下文。

阅读语言
中文译文

它为每个线程设置不同的优先级。生成线程以 PRIO_MIN 运行。其他线程几乎不占 CPU,以普通优先级运行。

#define THREAD_PRIORITY_LOWEST          PRIO_MIN
#define THREAD_PRIORITY_BELOW_NORMAL    2
#define THREAD_PRIORITY_NORMAL          0

由 Windows 优先级换算来的那些值,大概出自这样一张表:

"下表展示了 nice 值与 Win32 优先级之间的映射。Win32 优先级问题的更多信息参见 SetThreadPriority() 的 Win32 文档。

nice 值 Win32 优先级 -20 到 -16 THREAD_PRIORITY_HIGHEST -15 到 -6 THREAD_PRIORITY_ABOVE_NORMAL -5 到 +4 THREAD_PRIORITY_NORMAL +5 到 +14 THREAD_PRIORITY_BELOW_NORMAL +15 到 +19 THREAD_PRIORITY_LOWEST"

如果你有更好的取值,欢迎建议。

另外,网上有些建议说 Linux 上应该用 PRIO_PROCESS,因为线程即进程。如果这说法不成立,也许这就解释了为什么整个应用的优先级被意外设置。

// threads are processes on linux, so PRIO_PROCESS affects just the one thread setpriority(PRIO_PROCESS, getpid(), nPriority);

ORIGINAL · 英文原文
It sets different priorities for each thread.  The generate threads run at PRIO_MIN.  The other threads rarely take any CPU and run at normal.

#define THREAD_PRIORITY_LOWEST          PRIO_MIN
#define THREAD_PRIORITY_BELOW_NORMAL    2
#define THREAD_PRIORITY_NORMAL          0

The priorities converted from Windows priorities were probably from a table like this:

   "The following table shows the mapping between nice values and Win32 priorities. Refer to the Win32 documentation for SetThreadPriority() for more information on Win32 priority issues.

nice value    Win32 Priority
-20 to -16    THREAD_PRIORITY_HIGHEST
-15 to -6    THREAD_PRIORITY_ABOVE_NORMAL
-5 to +4    THREAD_PRIORITY_NORMAL
+5 to +14    THREAD_PRIORITY_BELOW_NORMAL
+15 to +19    THREAD_PRIORITY_LOWEST"

If you have better values, suggestions welcome.

Also, there was some advice on the web that PRIO_PROCESS is used on Linux because threads are processes.  If that's not true, maybe it accounts for unexpectedly setting the priority of the whole app.

    // threads are processes on linux, so PRIO_PROCESS affects just the one thread
    setpriority(PRIO_PROCESS, getpid(), nPriority);
来源
Bitcointalk 原始链接 ↗ 记录编号 SN-0433