c++ - Linux SCHED_FIFO 不考虑线程优先级
问题描述
设想
我创建了三个线程,固定到一个核心,具有以下优先级SCHED_FIFO
:
- 主要:
sched_priority = 99
- 线程_1:
sched_priority = 97
- 线程_2:
sched_priority = 98
工作线程 ( thread_1
, thread_2
) 计算 50,000,000 个素数的总和(约 10 秒)。它们直到结束(打印输出)才阻塞或执行系统调用。
主线程休眠一秒钟,然后检查工作线程的 Promise 以查看是否完成。
预期行为
主线程处于最高优先级。根据sched:
SCHED_FIFO 线程一直运行,直到它被 I/O 请求阻塞、被更高优先级的线程抢占或调用 sched_yield(2)。
因此,Main 应checking ...
以秒为间隔打印 ( )。它是最高优先级,因此应该抢占任何正在运行的东西。当它休眠时,它是阻塞的,所以其他线程应该运行。
thread_1
: 先完成,因为main不忙时优先。thread_2
:最后完成,只有在thread_1
完全完成后才开始。
实际行为
线程以与预期相反的顺序完成:
Thread 1 summed 3001134 primes at priority level: 97
Thread 2 summed 3001134 primes at priority level: 98
Main: Checking ...
Main: Task 1 has finished!
Main: Task 2 has finished!
Main: Exiting at priority level: 99
颠倒优先级顺序以使main具有最低的结果会产生完全相同的结果。
复制
- 编译程序
g++ -o <exec_name> <file_name>.cpp -pthread
- 运行:
sudo taskset --cpu-list 1 ./<exec_name>
我的内核是5.4.0-42-generic
,我的发行版(如果重要的话):Ubuntu 18.04.5 LTS
. 我没有安装preempt-rt
补丁。
类似问题
示例代码
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <future>
#include <pthread.h>
#include <math.h>
// IO Access mutex
std::mutex g_mutex_io;
// Computation function (busy work)
static bool isPrime (unsigned int value)
{
unsigned int i, root;
if (value == 1) return false;
if (value == 2) return true;
if ((value % 2) == 0) return false;
root = (int)(1.0 + sqrt(value));
for (i = 3; (i < root) && (value % i != 0); i += 2);
return (i < root ? false : true);
}
// Thread function
void foo (unsigned int id, unsigned int count)
{
sched_param sch;
int policy, sum = 0;
// Get information about thread
pthread_getschedparam(pthread_self(), &policy, &sch);
// Compute primes
for (unsigned int i = 1; i < count; ++i) {
sum += (isPrime(i) ? 1 : 0);
}
// Print
{
std::lock_guard<std::mutex> lock(g_mutex_io);
std::cout << "Thread " << id << " summed " << sum << " primes"
<< " at priority level: " << sch.sched_priority << std::endl;
}
}
int main ()
{
sched_param sch;
int policy;
// Declare and init task objects
std::packaged_task<void(unsigned int, unsigned int)> task_1(foo);
std::packaged_task<void(unsigned int, unsigned int)> task_2(foo);
// Get the futures
auto task_fut_1 = task_1.get_future();
auto task_fut_2 = task_2.get_future();
// Declare and init thread objects
std::thread thread_1(std::move(task_1), 1, 50000000);
std::thread thread_2(std::move(task_2), 2, 50000000);
// Set first thread policy
pthread_getschedparam(thread_1.native_handle(), &policy, &sch);
sch.sched_priority = 97;
if (pthread_setschedparam(thread_1.native_handle(), SCHED_FIFO, &sch)) {
std::cerr << "pthread_setschedparam: " << std::strerror(errno)
<< std::endl;
return -1;
}
// Set second thread policy
pthread_getschedparam(thread_2.native_handle(), &policy, &sch);
sch.sched_priority = 98;
if (pthread_setschedparam(thread_2.native_handle(), SCHED_FIFO, &sch)) {
std::cerr << "pthread_setschedparam: " << std::strerror(errno)
<< std::endl;
return -1;
}
// Set main process thread priority
pthread_getschedparam(pthread_self(), &policy, &sch);
sch.sched_priority = 99;
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sch)) {
std::cerr << "pthread_setschedparam: " << std::strerror(errno)
<< std::endl;
return -1;
}
// Detach these threads
thread_1.detach(); thread_2.detach();
// Check their status with a timeout
for (int finished = 0; finished < 2; ) {
std::this_thread::sleep_for(std::chrono::seconds(1));
{
std::lock_guard<std::mutex> lock(g_mutex_io);
std::cout << "Main: Checking ..." << std::endl;
}
if (task_fut_1.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
{
std::lock_guard<std::mutex> lock(g_mutex_io);
std::cout << "Main: Task 1 has finished!" << std::endl;
}
finished++;
}
if (task_fut_2.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
{
std::lock_guard<std::mutex> lock(g_mutex_io);
std::cout << "Main: Task 2 has finished!" << std::endl;
}
finished++;
}
}
pthread_getschedparam(pthread_self(), &policy, &sch);
std::cout << "Main: Exiting at priority level: " << sch.sched_priority << std::endl;
return 0;
}
实验
用两个内核运行这个程序sudo taskset --cpu-list 1,2
会产生以下奇怪的输出:
Thread 2 computed 3001134 primes at priority level: 98
Thread 1 computed 3001134 primes at priority level: 0
Main: Checking ...
Main: Task 1 has finished!
Main: Task 2 has finished!
Main: Exiting at priority level: 99
的优先级thread_1
为零。
如果我将其扩展为包括三个 cores sudo taskset --cpu-list 1,2,3
,那么我会得到我期望的单核行为:
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Main: Checking ...
Thread 2 computed 3001134 primes at priority level: 98
Thread 1 computed 3001134 primes at priority level: 0
Main: Checking ...
Main: Task 1 has finished!
Main: Task 2 has finished!
Main: Exiting at priority level: 99
重新排列配置优先级的顺序,以便主线程先完成,不会改变原始场景中的输出
解决方案
当你启动两个线程
// Declare and init thread objects
std::thread thread_1(std::move(task_1), 1, 50000000);
std::thread thread_2(std::move(task_2), 2, 50000000);
他们可以(!)立即运行并获取调度参数
// Get information about thread
pthread_getschedparam(pthread_self(), &policy, &sch);
甚至在您将它们设置pthread_setschedparam()
为另一个值之前。如果两个线程都相应地调度,输出甚至可能显示 0 和 0。
在主线程设置优先级之后,子线程可能(!)都被调度。然后你会得到预期的输出。但任何结果都是可能的。
当您pthread_getschedparam()
在输出之前移动到线程的末尾时,您更有可能获得预期的输出 97 和 98。但即使这样,两个线程也可能运行到最后,甚至在主线程被安排设置优先级之前.
推荐阅读
- import - 如何覆盖实体类以向导入类型的下拉列表添加新选项?PrestaShop 1.7
- sql-server - SQL 将字符串拆分为列
- python - 在 GEOPANDA 下制作复杂的过滤器
- reactjs - 如何在 Jest 中从 react-i18next 模拟 Trans 组件
- cloud-init - 云初始化运行命令中的列表与字符串
- powershell - 找到字符串后使用 Powershell 将内容添加到文本日志文件
- django - Django将默认值添加到具有另一个字段值的模型字段
- azure-data-factory-2 - 将文件移动到 ADFv2 中的不同访问层
- javascript - 检查 div 元素的子元素的顺序是否正确
- r - ggplot 仅显示 geom_line() 时间序列中的一些数据