禁用所有线程默认值的核心关联

Disable core affinity for all thread defaults

我在我的应用程序中使用 ffmpeg,它在内部产生了很多线程。但是,我希望我的具有实时语义的应用程序本身具有 cpu 核心之一。所以基本上我需要一种方法来禁用将 ffmpeg 生成的线程调度到核心并将我的主线程的亲和力设置为该核心。

这可能吗?

例如

main()
{
    struct sched_param param;
    param.sched_priority = 95;
    sched_setscheduler(getpid(), SCHED_FIFO, &param);

    int core_id = 0;

    // TODO: Remove core_id from cpuset for all future thread default affinities.

    pthread_t ffmpeg_thread;
    pthread_create(&ffmpeg_thread, NULL, run_ffmpeg, NULL);

    pthread_t rt_thread;
    pthread_create(&rt_thread, NULL, run_rt, NULL);

    cpu_set_t rt_cpuset;
    CPU_ZERO(&rt_cpuset);
    CPU_SET(core_id, &rt_cpuset);
    pthread_setaffinity_np(rt_thread, sizeof(rt_cpuset), &rt_cpuset);

    pthread_join(ffmpeg_thread, NULL);
    pthread_join(rt_threadm, NULL);
}

线程从调用线程继承它们的关联。

pthread_create

Linux-specific details The new thread inherits copies of the calling thread's capability sets (see capabilities(7)) and CPU affinity mask (see sched_setaffinity(2)).

因此,只需根据需要为两个线程设置亲和力,就会导致任何额外生成的线程继承它们的亲和力。这正是我想要的。