Skip to content

Use static thread_local instead of a function scope static variables in oclCvtColorBGR2HSV (color_hsv.dispatch.cpp) #24443

@kallaballa

Description

@kallaballa

System Information

OpenCV version: 4.8.0
Operating System / Platform: openSUSE Tumbleweed (VERSION_ID="20231008")
Compiler & compiler version: gcc (SUSE Linux) 13.1.1 20230710 [revision f9781fcf733ca3afe5b5811afd8ecbb18d9d3a11]

Detailed description

Inside the function oclCvtColorBGR2HSV (color_hsv.dispatch.cpp) variables are defined as static so that they are initialized only once. That can lead to race conditions and OpenCL-context conflicts. Instead of static these variables should be declared static thead_local;

Steps to reproduce

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <thread>
int main() {
	std::thread t([](){
		cv::UMat src(cv::Size(1280, 720), CV_8UC3);
		cv::UMat dst;
		while(true) {
			cvtColor(src, dst, cv::COLOR_BGR2HSV);
		}
	});

	cv::UMat src(cv::Size(1280, 720), CV_8UC3);
	cv::UMat dst;

	while(true) {
		cvtColor(src, dst, cv::COLOR_BGR2HSV);
	}
}

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions