How to prevent backgound tasks from starving main tasks in rayon threadpool?

Imagine that I am writing a game that uses rayon to execute main gameplay loop.

Sometimes, I need to spawn long-running tasks which can themselves be parallelized (e.g. saving and loading game, or loading and generating a level, or parsing and preparing assets, compiling shaders).

I would like to make those tasks utilise as much threads possible while keeping at least 1 or maybe 2 threads for main gameplay loop to avoid complete unresponsiveness for users (though I don't mind losing some FPS due to lack of parallelization).

Main loop is running inside rayon threadpool because I found out that this improves latency between spawning some tasks and continuing after their finish.

The problem
I don't see how can I make rayon threadpool reserve some threads for main loop. As I see, it is completely possible for tasks from background job to fill job queues for all threads so they would run them while main loop tasks would do nothing.

My current solution and question

I currently decided to create a temporary threadpool for background tasks which would have higher thread priority (set by OS API) but less threads than main pool (e.g. num_cores - 1 ). Since OS would prioritise background pool threads over main pool, background tasks would get more CPU time while still allowing main loop to progress using single left-out core. After task is done, I would be able to just drop background pool so OS would reclaim those threads.

Can this task be solved somehow better?
I was thinking if it is possible to spawn tasks in rayon that allowed to run only on some threads from pool.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.