简体   繁体   中英

Disable core affinity for all thread defaults

I'm using ffmpeg in my application which internally spawns lots of threads. However, I would like that my application which has real-time semantics has one of the cpu cores for itself. So basically I need a way to disable scheduling of ffmpeg spawned threads to a core and set the affinity of my main thread to that core.

Is this possible?

eg

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);
}

Threads inherit their affinity from the calling thread.

See 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)).

So just setting the affinity for both threads as wanted will cause any additionally spawned threads to inherit their affinity. Which would do exactly what I want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM