I have a C++ program that spawns processes. The user tells me how many cores we have to work with, and how many tasks they would like to run.
Say the user tells me they have 8 cores to work with, and they need to run 20 of our tasks. Then I spawn 8 tasks initially, and as each one of them finishes, I spawn a new task until we finish the 20 tasks.
My program works on Windows (MSVC 11) and Linux (GCC 4.8), so the implementations differ for those platforms, but in general, I'm using semaphores to keep track of the spawned processes.
My question is: what's a reasonable way to limit the number of cores the user can use? I would like to error out at the beginning of the program if the user tells us some number of cores that's not really feasible to work with. But I'm not really sure how to get a concrete number since it depends on the user's machine and the platform.
For example, I know that for Windows, I use WaitForMultipleObjects()
and so I'm limited to MAXIMUM_WAIT_OBJECTS
= 64 processes I can wait on (see here). But is there anything like this for Linux? Or even other "gotchas" like this in Windows?
I guess I'm looking for some standard good practices for setting such a limit.
Thanks.