I can produce this, only in OpenCV 3.3, only when using the pthreads parallel framework.
First If I setNumThreads (3) then, I change this value to setNumThreads (0), then I change the value again to setNumThreads (3), then the last action has no effect, and application using one thread. In general, after I set the value of setNumThreads (0), all future changes to setNumThreads() do not give an effet.
test code for produce:
#include <opencv2/opencv.hpp>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
cv::CascadeClassifier* cascadeFrontalFace;
cv::Mat image;
std::vector<cv::Rect> detectFace(){
std::vector<cv::Rect> dets;
cascadeFrontalFace->detectMultiScale(image,dets, 1.2, 6,0,cv::Size(20,20));
return dets;
}
int main(int argc, char *argv[])
{
std::cout<< cv::getBuildInformation()<<endl;
image = cv::imread("/home/nick64/testgstreamer/3/video12z8.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cascadeFrontalFace=new cv::CascadeClassifier("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml");
int tcnt[]={3,0,3};
for(int i=0; i<3; ++i){
milliseconds startTime = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
for(int n=0; n<10; ++n){
cv::setNumThreads(tcnt[i]);
std::vector<cv::Rect> dets;
cascadeFrontalFace->detectMultiScale(image,dets, 1.2, 6,0,cv::Size(20,20));
std::cout << "face detected:" << dets.size()<<endl;
}
milliseconds endTime = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
std::cout<< "between time: " << (endTime-startTime).count() << "num threads: " << cv::getNumThreads() <<endl;
}
delete cascadeFrontalFace;
}
Output for OpenCV 3.3:
General configuration for OpenCV 3.3.0 =====================================
Version control: unknown
Extra modules:
Location (extra): /media/nick64/1c6afc58-05d2-4957-802e-352b30bdedf0/dernel_ubuntu1610/OpenCv3.3/332/opencv-3.3.0/contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2017-09-22T11:06:39Z
Host: Linux 4.4.0.intel.r5.0 x86_64
CMake: 3.5.2
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release
CPU/HW features:
Baseline: SSE SSE2 SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
requested: AVX2
Dispatched code generation:
requested: AVX AVX2
C/C++:
Built as dynamic libs?: YES
C++11: YES
C++ Compiler: /usr/bin/x86_64-linux-gnu-g++ (ver 6.2.0)
C++ flags (Release): -g -O2 -fdebug-prefix-map=/media/nick64/1c6afc58-05d2-4957-802e-352b30bdedf0/dernel_ubuntu1610/OpenCv3.3/332/opencv-3.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mf16c -mfma -mavx -mavx2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -g -O2 -fdebug-prefix-map=/media/nick64/1c6afc58-05d2-4957-802e-352b30bdedf0/dernel_ubuntu1610/OpenCv3.3/332/opencv-3.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mf16c -mfma -mavx -mavx2 -fvisibility=hidden -fvisibility-inlines-hidden -g -DDEBUG -D_DEBUG
C Compiler: /usr/bin/x86_64-linux-gnu-gcc
C flags (Release): -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mf16c -mfma -mavx -mavx2 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mf16c -mfma -mavx -mavx2 -fvisibility=hidden -g -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
Linker flags (Debug): -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
ccache: NO
Precompiled headers: NO
Extra dependencies: /usr/lib/libopenblas.so gtk-3 gdk-3 pangocairo-1.0 pango-1.0 atk-1.0 cairo-gobject cairo gdk_pixbuf-2.0 gio-2.0 gthread-2.0 /usr/lib/x86_64-linux-gnu/libwebp.so /usr/lib/x86_64-linux-gnu/libjasper.so /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so /usr/lib/libgdal.so gstbase-1.0 gstreamer-1.0 gobject-2.0 glib-2.0 gstvideo-1.0 gstapp-1.0 gstriff-1.0 gstpbutils-1.0 dc1394 avcodec avformat avutil swscale avresample gphoto2 gphoto2_port vtkRenderingOpenGL vtkImagingHybrid vtkIOImage vtkCommonDataModel vtkCommonMath vtkCommonCore vtksys vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkDICOMParser vtkIOCore vtkmetaio /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libtiff.so vtkImagingCore vtkRenderingCore vtkCommonColor vtkFiltersExtraction vtkFiltersCore vtkFiltersGeneral vtkCommonComputationalGeometry vtkFiltersStatistics vtkImagingFourier vtkalglib vtkFiltersGeometry vtkFiltersSources vtkInteractionStyle vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOGeometry vtkFiltersTexture vtkRenderingFreeType /usr/lib/x86_64-linux-gnu/libfreetype.so vtkftgl vtkIOExport vtkRenderingAnnotation vtkImagingColor vtkRenderingContext2D vtkRenderingGL2PS vtkRenderingContextOpenGL /usr/lib/x86_64-linux-gnu/libgl2ps.so vtkRenderingLabel freetype harfbuzz /usr/lib/x86_64-linux-gnu/hdf5/serial/lib/libhdf5.so /usr/lib/x86_64-linux-gnu/libpthread.so /usr/lib/x86_64-linux-gnu/libsz.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libm.so tesseract lept dl m pthread rt
3rdparty dependencies:
OpenCV modules:
To be built: core flann hdf imgproc ml objdetect phase_unwrapping photo plot reg surface_matching video viz xphoto bgsegm dnn face freetype fuzzy img_hash imgcodecs shape videoio xobjdetect highgui superres ts bioinspired dpm features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco java optflow stitching python2 python3
Disabled: world contrib_world dnn_modern sfm
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cnn_3dobj cvv matlab
GUI:
QT: NO
GTK+ 3.x: YES (ver 3.20.9)
GThread : YES (ver 2.50.2)
GtkGlExt: NO
OpenGL support: NO
VTK support: YES (ver 6.3.0)
Media I/O:
ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x0209)
PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.25)
TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.6)
JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
GDAL: /usr/lib/libgdal.so
GDCM: NO
Video I/O:
DC1394 1.x: NO
DC1394 2.x: YES (ver 2.2.4)
FFMPEG: YES
avcodec: YES (ver 57.24.102)
avformat: YES (ver 57.25.100)
avutil: YES (ver 55.17.103)
swscale: YES (ver 4.0.100)
avresample: YES (ver 3.0.0)
GStreamer:
base: YES (ver 1.8.3)
video: YES (ver 1.8.3)
app: YES (ver 1.8.3)
riff: YES (ver 1.8.3)
pbutils: YES (ver 1.8.3)
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: NO/YES
XIMEA: NO
Xine: NO
Intel Media SDK: NO
gPhoto2: YES
Parallel framework: pthreads
Trace: YES (with Intel ITT)
Other third-party libraries:
Use Intel IPP: 2017.0.2 [2017.0.2]
at: /media/nick64/1c6afc58-05d2-4957-802e-352b30bdedf0/dernel_ubuntu1610/OpenCv3.3/332/opencv-3.3.0/obj-x86_64-linux-gnu/3rdparty/ippicv/ippicv_lnx
Use Intel IPP IW: prebuilt binaries (2017.0.2)
Use Intel IPP Async: NO
Use VA: NO
Use Intel VA-API/OpenCL: NO
Use Lapack: YES (/usr/lib/libopenblas.so)
Use Eigen: YES (ver 3.2.93)
Use Cuda: NO
Use OpenCL: YES
Use OpenVX: NO
Use custom HAL: NO
OpenCL: <Dynamic loading of OpenCL library>
Include path: /opt/intel/opencl/include/CL
Use AMDFFT: NO
Use AMDBLAS: NO
Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.12)
Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12+)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.1rc1)
packages path: lib/python2.7/dist-packages
Python 3:
Interpreter: /usr/bin/python3 (ver 3.5.2)
Libraries: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2+)
numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.11.1rc1)
packages path: lib/python3.5/dist-packages
Python (for build): /usr/bin/python2.7
Java:
ant: /usr/bin/ant (ver 1.9.7)
JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
Java wrappers: YES
Java tests: NO
Matlab: Matlab not found or implicitly disabled
Documentation:
Doxygen: /usr/bin/doxygen (ver 1.8.11)
Tests and samples:
Tests: NO
Performance tests: YES
C/C++ Examples: YES
Install path: /usr
cvconfig.h is in: /media/nick64/1c6afc58-05d2-4957-802e-352b30bdedf0/dernel_ubuntu1610/OpenCv3.3/332/opencv-3.3.0/obj-x86_64-linux-gnu
-----------------------------------------------------------------
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 7587num threads: 3
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 15809num threads: 1
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 15803num threads: 3
Output for OpenCV 3.2 text:
General configuration for OpenCV 3.2.0 =====================================
Version control: unknown
Platform:
Timestamp: 2017-06-20T16:46:00Z
Host: Linux 4.8.0-56-generic x86_64
CMake: 3.5.2
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release
C/C++:
Built as dynamic libs?: YES
C++ Compiler: /usr/bin/c++ (ver 6.2.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mavx -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -mf16c -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mavx -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -mf16c -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mavx -ffunction-sections -fvisibility=hidden -mf16c -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mavx -ffunction-sections -fvisibility=hidden -mf16c -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release):
Linker flags (Debug):
ccache: NO
Precompiled headers: YES
Extra dependencies: /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libtiff.so /usr/lib/x86_64-linux-gnu/libjasper.so /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so gtk-x11-2.0 gdk-x11-2.0 pangocairo-1.0 atk-1.0 cairo gdk_pixbuf-2.0 gio-2.0 pangoft2-1.0 pango-1.0 gobject-2.0 glib-2.0 fontconfig freetype gthread-2.0 gstbase-1.0 gstreamer-1.0 gstvideo-1.0 gstapp-1.0 gstriff-1.0 gstpbutils-1.0 dc1394 avcodec avformat avutil swscale dl m pthread rt /usr/lib/libopenblas.so
3rdparty dependencies: libwebp
OpenCV modules:
To be built: core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python2
Disabled: world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3 viz
GUI:
QT: NO
GTK+ 2.x: YES (ver 2.24.30)
GThread : YES (ver 2.50.2)
GtkGlExt: NO
OpenGL support: NO
VTK support: NO
Media I/O:
ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
WEBP: build (ver 0.3.1)
PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.25)
TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.6)
JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
GDAL: NO
GDCM: NO
Video I/O:
DC1394 1.x: NO
DC1394 2.x: YES (ver 2.2.4)
FFMPEG: YES
avcodec: YES (ver 57.24.102)
avformat: YES (ver 57.25.100)
avutil: YES (ver 55.17.103)
swscale: YES (ver 4.0.100)
avresample: NO
GStreamer:
base: YES (ver 1.8.3)
video: YES (ver 1.8.3)
app: YES (ver 1.8.3)
riff: YES (ver 1.8.3)
pbutils: YES (ver 1.8.3)
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: NO/YES
XIMEA: NO
Xine: NO
gPhoto2: NO
Parallel framework: pthreads
Other third-party libraries:
Use IPP: 9.0.1 [9.0.1]
at: /home/nick64/Downloads/OpenCC32/opencv-3.2.0/build/3rdparty/ippicv/ippicv_lnx
Use IPP Async: NO
Use VA: NO
Use Intel VA-API/OpenCL: NO
Use Lapack: YES (/usr/lib/libopenblas.so)
Use Eigen: NO
Use Cuda: NO
Use OpenCL: YES
Use OpenVX: NO
Use custom HAL: NO
OpenCL: <Dynamic loading of OpenCL library>
Include path: /home/nick64/Downloads/OpenCC32/opencv-3.2.0/3rdparty/include/opencl/1.2
Use AMDFFT: NO
Use AMDBLAS: NO
Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.12)
Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12+)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.1rc1)
packages path: lib/python2.7/dist-packages
Python 3:
Interpreter: /usr/bin/python3 (ver 3.5.2)
Python (for build): /usr/bin/python2.7
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Matlab: Matlab not found or implicitly disabled
Documentation:
Doxygen: NO
Tests and samples:
Tests: YES
Performance tests: YES
C/C++ Examples: NO
Install path: /usr/local
cvconfig.h is in: /home/nick64/Downloads/OpenCC32/opencv-3.2.0/build
-----------------------------------------------------------------
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 7685num threads: 3
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 16246num threads: 1
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
face detected:2
between time: 7324num threads: 3
Here first i use 3 thread, and it working in 7 second, next i set 0 thread, it use 1 thread for detection, next i again set 3 - but application work 15 second lik, if i use one thread, but getNumThread return value 3! All future sets this value is not take effect. If i set value 3,1,3 then it work fine, only setNumThread(0) produce it, and only in OpenCV 3.3 version.