16

I have an application that I need to run multithreaded but I want it to use only one core of the computer, as if my computer has single core (I know the behavior of multithreaded application on a computer with single core), although it is not.

This application is going to be deployed on a customer computer (Windows XP & 7) and I don't want my application to use more than one core. Can this be done? Does it depend on the programming language? Or all the thread management is left to the OS?

Thanks in advance.

ρss
  • 5,115
  • 8
  • 43
  • 73
guneykayim
  • 5,210
  • 2
  • 29
  • 61

2 Answers2

4

You can set affinity of the whole program to bind to just one cpu.

In unix you can use taskset but for windows I only know how to do it from task manager which might not suit you.

I have a library, Java Thread Affinity which will allow you to set the affinity programatically.

AffinitySupport.setAffinity(1); // only run on cpu 0.

This will also limit any thread started from that point to the same affinity.

You could add a class with a main() which sets the affinity and calls your normal main() allowing you to add this without altering any of your existing code.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

If you don't want to change your program source you can use the OS commands

On Linux you can use the tool cpulimit. More details here and here

On Windows you can use /AFFINITY parameter to the start command. More details here

Community
  • 1
  • 1
Vlad
  • 9,180
  • 5
  • 48
  • 67