Out-of-box distribution will automatically select the optimal number of CPU threads to use (usually the selection is made to utilize all CPU resources available). However, nowadays it is quite common to build multi-threaded applications, so that parallelism is implemented not by internal libraries, but at the application level. In that case multi-threaded environments may conflict in terms of performance, so it may slow down your application.
Some real-life examples of this case:
- mobile applications, where all the computationally expensive work is usually performed asynchronously
- server software, where each client is being served with a separate thread
There are two ways to override the default number of CPU threads setting:
- Use the
SetNumThreads
API call - Set the
DOCSDK_NUM_THREADS_PIPELINE
,DOCSDK_NUM_THREADS_ENGINE
andDOCSDK_NUM_THREADS_OPERATOR
environment variables to the same value
Note
SetNumThreads
function call takes precedence over environment variables setting.
Despite that SetNumThreads
can be successfully called at any runtime moment, this method should be called before the liveness pipeline initialization. Otherwise the changes won't be applied to the liveness pipeline instance. Please take a look at the right column for the correct call sequence.
Note
With any number of threads setting, any IDLive Doc SDK class or method is thread-safe.
Below sample programs illustrate the correct way of setting up number of CPU threads using SDK API:
#include <docsdk/settings.h>
#include <docsdk/liveness_pipeline.h>
int main() {
docsdk::SetNumThreads(6);
auto pipeline = docsdk::LivenessPipeline::Create("/path/to/config.json");
return 0;
}
#include <docsdk/c_api.h>
int main(void) {
DocSdkSetNumThreads(6);
DocSdkErrorCode error_code;
DocSdkLivenessPipeline *pipeline = DocSdkCreateLivenessPipeline("/path/to/config.json", &error_code)
return 0;
}
package net.idrnd.docsdk.examples;
import net.idrnd.docsdk.LivenessPipeline;
import net.idrnd.docsdk.Settings;
public class LivenessPipelineExample {
public static void main(String[] args) {
Settings.setNumThreads(6);
LivenessPipeline pipeline = new LivenessPipeline("/path/to/config.json");
}
}
from DocSdk import LivenessPipeline, set_num_threads
set_num_threads(6)
pipeline = LivenessPipeline("/path/to/config.json")