gpt4 book ai didi

c - 以非常低的优先级运行线程

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:08 25 4
gpt4 key购买 nike

我使用 pthread_create() 在我的 Linux 应用程序中创建了一个线程。我想让这个线程以非常非常低的优先级运行,因为有一些实时线程在同一个应用程序中运行。以下是我在线程函数本身中的代码:

        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

/* Trying to set lowest priority possible for this thread */
param.sched_priority = 0;
ret = pthread_setschedparam(pthread_self(), SCHED_IDLE, &param);
if (!ret)
printf("Successfully set sched policy of thread\n");

我想确认上面的代码是否正确。与其他实时线程相比,它是否保证我的线程不会获得更高的优先级。请建议是否需要任何更改。仅供引用,代码在嵌入式平台上运行。

最佳答案

我假设您已经使用了 Default Linux 分时调度。因为你添加了 param.sched_priority = 0;

为此,您可以查看 Here

指出

   SCHED_OTHER can be used at only static priority 0 (i.e., threads
under real-time policies always have priority over SCHED_OTHER pro‐
cesses). SCHED_OTHER is the standard Linux time-sharing scheduler
that is intended for all threads that do not require the special
real-time mechanisms.
   The thread to run is chosen from the static priority 0 list based on
a dynamic priority that is determined only inside this list. The
dynamic priority is based on the nice value (see below) and is
increased for each time quantum the thread is ready to run, but
denied to run by the scheduler. This ensures fair progress among all
SCHED_OTHER threads.

关于c - 以非常低的优先级运行线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com