goocreations
goocreations

Reputation: 3066

Linux - Limit threads per process

I'ce written a C++ program that does some benchmarking on a couple of algorithms. Some of these algorithms are using other libraries for their calculations. These external libraries (which I don't have control over) use multi-threading, which makes it difficult to get a proper benchmark (some algorithms are single-threaded, some are multi-threaded).

So when doing the benchmarking, I want to limit the threads to 1. Is there anyway I can start a program in Linux and tell it to use a maximum of 1 thread, instead of the default in the external libraries (which is equal to the number of cores)?

Upvotes: 2

Views: 396

Answers (1)

jsantander
jsantander

Reputation: 5102

I'm not sure it is possible what you ask from an OS perspective [what could the OS do? return failure when the next thread is requested?].

I'd search in the libraries documentation, they will probably provide configuration values for the number of threads they're using.

Alternatively, you can do processor affinity using the taskset command

It is not exactly what you requested, but it ensures that your program will be running on a given CPU (or a set of CPUs). So regardless of the number of threads spawned, the program will be using at any time at most one CPU (or the number of CPUs specified), so the effective parallelism will be controlled.


Alternatively x2 Triggered by the comment from @goocreations... One way of fooling the programs into believing they have a different number of CPUs available is to run them in a virtual machine (e.g. VirtualBox)

Upvotes: 2

Related Questions