So basically I am trying to set an environment variable in my C++ code to force the number of threads to be 1. I am using multiple machine learning libraries which by default use OpenMP and can be forced to operate in single thread mode by setting the following environment variable: OMP_NUM_THREADS=1
Here's my issue. I want to set this environment variable from within a library I am building.
When I set the environment variable from my main function (executable linking against the library I am building) then it works as expected (only 1 thread is used during the execution of the program):
auto implPtr = FRVT_11::Interface::getImplementation();
implPtr->initialize(CONFIG_DIR);
char ompEnv[]="OMP_NUM_THREADS=1";
putenv( ompEnv );
// More code
However, if I try to set the environment variable from within the library I am building (for example from within the getImplementation function), then the number of threads used is 4 instead of 1:
// This does not work
std::shared_ptr<FRVT_11::Interface> FRVT_11::Interface::getImplementation() {
char ompEnv[]="OMP_NUM_THREADS=1";
putenv( ompEnv );
return std::make_shared<MyImplementation>();
}
Any ideas why this would be the case? I am building and shipping the library so I need the number of threads to be set from within the library.