Repository: opencv
Issue: "Memory leak" when CV_TRACE=ON is combined with pthreads
Labels: priority: low,category: core

##### System information (version)
- OpenCV => 4.5.0/4.5.1
- Operating System / Platform => Ubuntu 16.04/20.10
- Compiler => GCC 5.4/10.2

##### Detailed description

Seems like OpenCV does not release memory from its internal thread local storage until the process ends even if all the corresponding threads have already exited. Apparently the memory is actually freed on the program exit, because jemalloc does not detect any leaks, however it's not sufficient for server programs using OpenCV since they should operate constantly for a long time without a restart. Thus this is not really a memory leak in classic terms, but it acts like this from the perspective of the application which wraps OpenCV.

This also seems to be somehow related to the OpenCV tracing mechanism, specifically to the CV_TRACE build option: if it's disabled on the build stage, then no "memory leak" happens. This provides a way to workaround the issue, however CV_TRACE is enabled by default, so inexperienced OpenCV user may not figure it out.

The issue caption mentions pthreads, because C++ STL on Linux uses pthreads by default and the root cause of the issue is likely located somewhere in `opencv/modules/core/src/system.cpp` pthreads-specific code. Also disabling WITH_PTHREADS_PF build option also resolves the issue.

##### Steps to reproduce

Here is a simple command line program, which helps indicating the issue:
```cpp
#include <iostream>
#include <thread>

#include <opencv2/opencv.hpp>

int main(int argc, char **argv) {
    if (argc < 2) {
        std::cout << "Usage: " << argv[0] << " <use_threads: 1|0>" << std::endl;
        return 0;
    }

    const bool use_threads = std::stoi(argv[1]);

    cv::Mat img(512, 512, CV_8UC3);
    cv::randu(img, cv::Scalar(0, 0, 0), cv::Scalar(255, 255, 255));

    auto callable = [&img]() {
        cv::Mat img2;
        cv::resize(img, img2, cv::Size(), 0.5, 0.5);
    };

    if (use_threads) {
        for (size_t i = 0; i < 10000; ++i) {
            std::thread { callable }.join();
        }
    } else {
        for (size_t i = 0; i < 10000; ++i) {
            callable();
        }
    }

    return 0;
}
```

Disclaimer for the issue assignee: **I am aware of a memory fragmentation caused by spawning a lot of threads and not using a thread pool instead, but this approach is sometimes justified even in some real-world production systems.**

Here what we do in the above code:
1. Create an OpenCV matrix filled with random numbers.
2. Perform 10000 matrix resizes either in the main thread or in the spawned one-task thread deepening on the program args. As I saw from more complex experiments, `cv::resize` can be replaced by almost any OpenCV function with the same effect achieved.

In order to inspect memory consumption we will use simple [Python memory profiling utility](https://pypi.org/project/memory-profiler/). We will test four different running configurations (memory consumption plot is provided for each one of them):
1. OpenCV is built with CV_TRACE=1, `use_threads` is true.
![trace_use_threads_1](https://user-images.githubusercontent.com/5624568/111065662-d6172280-84cb-11eb-9270-387d4d2ba695.png)
2. OpenCV is built with CV_TRACE=1, `use_threads` is false.
![trace_use_threads_0](https://user-images.githubusercontent.com/5624568/111065664-d7e0e600-84cb-11eb-8cfd-ab804461449b.png)
3. OpenCV is built with CV_TRACE=0, `use_threads` is true.
![no_trace_use_threads_1](https://user-images.githubusercontent.com/5624568/111065666-d9121300-84cb-11eb-9849-f51b695f64f8.png)
4. OpenCV is built with CV_TRACE=0, `use_threads` is false.
![no_trace_use_threads_0](https://user-images.githubusercontent.com/5624568/111065668-da434000-84cb-11eb-8ba5-086b8ee90deb.png)

It can be clearly seen that memory consumption constantly increases than OpenCV is built with CV_TRACE=1, `use_threads` is set to true.

Is this kind of behavior is expected? If so, I have not found any documentations mentioning this.

Please let me know I there any additional information is required (e.g. OpenCV build script used).

##### Issue submission checklist

 - [x] I report the issue, it's not a question
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found solution
 - [x] I updated to latest OpenCV version and the issue is still there
 - [x] There is reproducer code and related data files: videos, images, onnx, etc