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

标签 c++ c linux pthreads affinity

我在我的应用程序中使用 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)).

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

关于c++ - 禁用所有线程默认值的核心关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516163/

相关文章:

c - 标记一个 c 字符串

c - Linux操作系统如何在有多个套接字时调度线程

c++ - 为什么所有这些元组的大小不一样

c++如何编写编译器可以轻松针对SIMD优化的代码?

c++ - Make - 无所事事(另一个)

c++ - 如何将 DeviceIOControl 移植到 ioctl?

linux - 返回小于给定日期的月结束日期,并在 shell 中停止于 12 个日期

c# - 询问一行的执行

c - C 中的简单连接

c - 如何确定 SQLBindCol 的正确缓冲区大小