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.
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.