Reduce contextual switching between threads with the same priority

I am writing an application using a third-party library to perform heavy computing.

This library implements parallelism internally and spawns given numeric streams. I want to run several instances (dynamic counting) of this library and, therefore, as a result I re-sign the processor a lot.

Is there any way to increase the "time slice" of all threads in a process so that, for example, all threads with normal priority rarely switch to context (exit), unless they are explicitly received through, for example, semaphores?

That way, I could avoid most of the performance overhead associated with over-subscribing the processor. Please note that in this case I don’t care if the thread is hungry for a few seconds.

EDIT:

One tricky way to do this is to manually schedule threads.

  • List all threads with a specific priority (for example, regular).
  • Pause them all.
  • Create a loop that resumes / pauses threads each, for example. 40 ms and ensures that no sea flows except the current processor counter will be executed.

Any serious flaws in this approach? Not sure what the overhead of resuming / stopping the flow?

+6
source share
2 answers

You don’t have to do anything special. Any decent scheduler will prevent forced context switches from consuming a significant portion of the CPU resources. Any operating system that does not have a decent scheduler should not be used.

Overhead of CPU re-signing performance is not an overhead for switches with context disabled. What for? Because the scheduler can simply avoid this. The scheduler only performs an unexpected context switch when it takes precedence. Performance Costs:

  • It may take longer to complete the task, because more work will be performed on other tasks between starting work and completing the task.

  • Additional threads consume memory for their stacks and other tracking information.

  • More threads tend to mean more conflicts (for example, when memory is allocated), which may mean more forceful context switches, where the thread must be disconnected because it cannot move forward.

    / li>

You just want to try to change the behavior of the scheduler when you know something significant that the scheduler does not know. Nothing of the kind happens here. So the default behavior is what you want.

+4
source

Any serious flaws in this approach? Not sure if the overhead resume / pause the stream?

Yes , resuming / pausing a thread is a very dangerous job done in user mode of the program . Therefore, it should not be used (almost never). Moreover, we should not use these concepts to achieve what any modern planner does for us. This is also mentioned in another article of this question.

The above applies to any operating system, but from the SO post tag it seems to me that he was offered the Microsoft Windows system . Now, if we read about SuspendThread () from MSDN, we get the following:

"This function is primarily intended for use by debuggers. It is not intended to synchronize threads. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to get a synchronization object that belongs to a suspended thread." .

So, consider a scenario in which a thread has acquired some resource (implicitly. Part of the code ... in library or kernel mode), and if we pause the thread, this will lead to a mysterious deadlock situation, as other threads of this process will wait for this particular resource. The fact is that we are not sure (at any time) in our program that what resources will be received by any running thread, pausing / resuming a thread is not a good idea.

+2
source

Source: https://habr.com/ru/post/974610/


All Articles