mousomer
mousomer

Reputation: 2858

Can I force C++ library to use single thread?

I am using a proprietary C++ library on linux, compiled through gcc, which uses pthreads (I have to use the -lpthreads flag on gcc). I have a wrapper around it, and I know that the library is using multiple threads.

The library uses multiple threads dynamically - when I call it I can see anywhere between 20 an 1 threads. But I don't want to use taskset. (I have other processes running and I want the system to administer cores).

Is there a way to force my executable to use single thread? Either on compile or on run time. Thanks.

EDIT: I can run the executable with taskset, and then cat /proc//status gives me:

State: R (running) Tgid: 1623 Pid: 1623 PPid: 31002 TracerPid: 0 Uid: 500 500 500 500 Gid: 100 100 100 100 Utrace: 0 FDSize: 256 Groups: 100 VmPeak: 346528 kB VmSize: 345956 kB VmLck: 0 kB VmHWM: 199816 kB VmRSS: 188388 kB VmData: 192120 kB VmStk: 128 kB VmExe: 656 kB VmLib: 12444 kB VmPTE: 432 kB VmSwap: 0 kB Threads: 1 SigQ: 2/62004 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000000004 SigCgt: 0000000180000000 CapInh: 0000000000000000 CapPrm: 0000000000000000 CapEff: 0000000000000000 CapBnd: ffffffffffffffff Cpus_allowed: 02 Cpus_allowed_list: 1 Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 Mems_allowed_list: 0 voluntary_ctxt_switches: 3460 nonvoluntary_ctxt_switches: 24907

So, apparently, it can run single thread.

Upvotes: 4

Views: 3480

Answers (3)

user9614249
user9614249

Reputation:

If you don't care about spawning threads and just want the program to behave sequentially, you can use numactl on linux.

Taken from this answer on SuperUser, you can restrict the program to executing one thread at a time by using

numactl --physcpubind=+1 /path/to/your/executable

From the numactl documentation:

--physcpubind=cpus, -C cpus

Only execute process on cpus. This accepts cpu numbers as shown in the processor fields of /proc/cpuinfo, or relative cpus as in relative to the current cpuset.

The program will still spawn threads, but is restricted to executing on a single CPU. This means only one thread can run at a time.

Upvotes: 5

Vijay
Vijay

Reputation: 67291

Dont know much about the internals of the process. But why dont you enhance your code base to restrict creating more than 1 thread. You can maintain a count of the threads and stop creating threads in the process when the count reaches more than 1.

Upvotes: 0

ElderBug
ElderBug

Reputation: 6145

Without knowing what you are trying to do, there is only one answer : You can't.

How would you restrain something to not use multiple threads ? Disabling pthread_create() would only cripple the program. Imagine that the lib spawns a thread to do some async work on a file. If you forbid pthread_create(), what happens to the file operations ? The lib now doesn't work properly, since it can't do its file operations. You would need a complete redesign of the lib ; just smashing the file IO on the same thread (if it was even possible) would probably be disastrous, since this thread was supposed to be free running, and now is blocked by some heavy IO.

That's the general idea. Regardless of whether the threads are really needed, if a lib was designed to be multi-thread, you can't simply make it single-thread. You can however use taskset, as you mentioned, or sched_setaffinity() (from C/C++) to run a process on a single CORE.

Upvotes: 1

Related Questions