0

I have an executable program that performs latency measurements. C++ pseudo-code below:

void main(){
  lock_priority();

  start_measurements();
  work();
  end_measurements();
}

The work() creates multiple threads and takes a long time to complete, so ideally I'd like to minimize the executable console when the process is running, just to save screen space. This, however, reduces the output latency by around 50% compared to when not minimized.

I'd like to implement the lock_priority() function so that even when minimized, the process does not go into PROCESS_MODE_BACKGROUND_BEGIN mode.

What I've tried so far

  1. SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); - did not work
  2. Created a thread that every few seconds calls the function above - it did work, but, scientifically speaking, "it looks ugly"
  3. I have tried to find a method to attach a callback to the SetPriorityClass() function so that after it finishes if the PriorityClass was anything but REALTIME_PRIORITY_CLASS, it'd re-set it again (or at least set PROCESS_MODE_BACKGROUND_END priority). This sounds like a perfect solution, but I could not find anything in the docs about this.
  4. I discovered there is a way to set the processor to prefer foreground/background tasks (reference) - however even if this was possible to be configured through code, I still need a way to bind this theoretical function to the priority change.

Any help would be very appreciated!

  • 1
    Is the code outputting to the console from multiple threads? If so this is a bad idea as console IO is synchronised and will limit the threads performance. – Richard Critten Oct 12 '22 at 08:13
  • No, the only output is in the final step, where end_measurements() prints the execution duration – Piotr Krzemiński Oct 12 '22 at 08:31
  • 1
    Can you provide a [mcve]? I know windows gives a boost to the foreground app, but it shouldn't impact a console application with realtime priority class "by 50%" when it runs in the background. Something else is going on. – selbie Oct 12 '22 at 09:47
  • 1
    When you run your app in REALTIME priority, you should be able to confirm that its running in this priority in TaskManager. By chance are you running on battery and/or with a power plan that isn't one of the "high" profiles. I'm just wondering if this is a laptop issue. – selbie Oct 12 '22 at 09:50
  • Windows version? – Anders Oct 12 '22 at 09:57

1 Answers1

2

How about redirecting the programm output from console to a file or just buffer it, like here:

Redirect both cout and stdout to a string in C++ for Unit Testing

This way, you don't have any console latency at all - if this is alright for your testing.

nick
  • 541
  • 1
  • 9
  • That is a good idea! However, running my built executable and minimizing it still causes the latency to drop significantly, which is why I believe the problem lies with the process prioritization on Windows. [Here is a reference to a post discussing this problem](https://superuser.com/questions/586101/windows-slowing-down-minimized-processes) – Piotr Krzemiński Oct 12 '22 at 08:37
  • @Piotr Krzemiński: Ah, ok. Have you tried running your program as a Windows Service under realtime prio ? You don't get a window then, so you'll some other way to talk to your application. But the foreground/background thing is no longer an issue. Might be worth a try... – nick Oct 12 '22 at 09:20