简体   繁体   中英

Preventing Windows from changing process affinity

I have a multithreaded code that I want to run on all 4 cores that my processor has. Ie I create four threads, and I want each of them to run on a separate core.

What happens is that it starts running on four cores, but occasionally would switch to only three cores. The only things running are the OS and my exe. This is somewhat disappointing, since it decreases performance by a quarter, which is significant enough for me.

The process affinity that I see in Task Manager allows the process to use any core. I tried restricting thread affinities, but it did't help. I also tried increasing priority of the process, but it did not help the case either.

So the question is, is there any way to force Windows to keep it running on all four cores? If this is not possible, can I reduce the frequency of these interruptions? Thanks!

This is not an issue of affinity unless I am very much mistaken. Certainly the system will not restrict your process to affinity with a specific set of threads. Some other program in the system would have to do that, if indeed that is happening.

Much more likely however is that, simply, there is another thread that is ready to run that the system is scheduling in a round-robin fashion. You have four threads that are always ready to run. If there is another thread that is ready to run, it will get its turn. Now there are 5 threads sharing 4 processors. When the other thread is running, only 3 of yours are able to run.

If you want to be sure that such other threads won't run then you need to do one of the following:

  1. Stop running the other program that wants to use CPU resource.
  2. Make the relative thread priorities such that your threads always run in preference to the other thread.

Now, of these options, the first is to be preferred. If you prioritize your threads above others, then the other threads don't get to run at all. Is that really what you want to happen?

In the question you say that there are no other processes running. If that is the case, and nobody is meddling with processor affinity, and only a subset of your threads are executing, then the only conclusion is that not all of your threads are ready to run and have work to do. That might happen if you, for instance, join your threads at the end of one part of work, before continuing on to the next.

Perhaps the next step for you is to narrow things down a little. Use a tool like Process Explorer to diagnose which threads are actually running.

If this is windows, try SetThreadAffinityMask():

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686247(v=vs.85).aspx

I would assume that if you only set a single bit, then that forces the thread to run only on the selected processor (core).

other process / thread functions:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs.85).aspx

I use a windows video program, and it's able to keep all the cores running at near max when rendering video.

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