Repository: opencv
Issue: Unable to execute cv2.imshow() the second time in Linux, works in Windows
Labels: bug

### System Information

OpenCV python version: 4.9.0.80
Operating System / Platform: Ubuntu 22.04.4 LTS / Windows 11 pro
Python version: 3.11

### Detailed description

I have another GUI script to open the Thread. When running the program in Linux, it has no issue when I first call cv2.imshow(), but after closing the cv2 window and reopen it, the script froze at cv2.imshow() (no error output). If I open a second thread while the first one is running, it will also freeze the first imshow(). 
None of these issues appears to happen when i run the scripts in Windows.

A few things I suspect: 
The thread management are different in Linux? 
imshow() does not close properly in Linux?



### Steps to reproduce

```python
class CaptureThread(threading.Thread):
    camera_index = None
    cap = None
    ret = None
    frame = None
    isRunning = False
    is_window_closed = False
    counter = 0
    timer = 0.0

    def request_stop(self):
        self.is_window_closed = True
        return

    def run(self):
        self.cap = cv2.VideoCapture(self.camera_index, cv2.CAP_V4L2)
        if global_vars.USE_CUSTOM_CAM_SETTINGS:
            self.cap.set(cv2.CAP_PROP_FPS, global_vars.FPS)
            self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, global_vars.WIDTH)
            self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, global_vars.HEIGHT)

        time.sleep(0.5)  # wait for camera to warm up
        print("Camera Index: ", self.camera_index)
        print("Opened Capture @ %s fps" % str(self.cap.get(cv2.CAP_PROP_FPS)))
        while not self.is_window_closed and not global_vars.KILL_THREADS:
            self.ret, self.frame = self.cap.read()  # read the image from the camera
            self.isRunning = True
            if global_vars.DEBUG:
                self.counter = self.counter + 1
                if time.time() - self.timer >= 3:
                    print("Capture FPS: ", self.counter / (time.time() - self.timer))
                    self.counter = 0
                    self.timer = time.time()

class BodyThread(threading.Thread):
    stop_requested = False
    camera_index = None

    def __init__(self, camera_index):
        threading.Thread.__init__(self)
        # self.pcal = PoseCalculations()
        # self.capture = CaptureThread()
        self.camera_index = camera_index
        self.window_name = "Body Tracking " + str(camera_index)

    def request_stop(self):
        self.stop_requested = True

    def run(self):
        # pull frame
        capture = CaptureThread()
        capture.camera_index = self.camera_index
        capture.start()
        try:
            pcal = PoseCalculations()
            # create landmarker
            pose_landmarker = pose_landmarker_and_result()
            while capture.isRunning == False:
                print("Waiting for camera and capture thread.")
                time.sleep(0.5)
            print("Beginning capture")

            while capture.cap.isOpened() and not self.stop_requested:
                ti = time.time()
                ret = capture.ret
                image = capture.frame
                # mirror frame
                image = cv2.flip(image, 1)
                # update landmarker results
                result = pose_landmarker.detect_sync(image)
                # calculate fps
                tf = time.time()
                pose_fps = 1 / (tf - ti)

                # draw landmarks and stats on frame
                image = pcal.draw_landmarks(image, result)
                show_stats(image, pose_fps, pcal.left_elbow_angle, pcal.right_elbow_angle,
                           pcal.left_wrist_speed, pcal.right_wrist_speed)
                pcal.pose_recognition()
                if capture.camera_index == 2:
                    print("passed pcal.recognition")
                    # print(str(frame))
                # display live camera
                cv2.imshow(self.window_name, image)
                cv2.waitKey(1)
                if capture.camera_index == 2:
                    print("showing window running 2")
                if cv2.waitKey(1) == ord('q'):
                    self.request_stop()  # Request the current thread to stop
                    capture.request_stop()  # Request the CaptureThread to stop
                    break
                # Check if the window was closed
                if cv2.getWindowProperty(self.window_name, cv2.WND_PROP_VISIBLE) < 1:
                    self.request_stop()  # Request the current thread to stop
                    capture.request_stop()  # Request the CaptureThread to stop
                    capture.cap.release()
                    pose_landmarker.close()
                    cv2.destroyAllWindows()
                    print("destroyAllWindows X")
                    break  # Exit the loop
        finally:
            # release everything
            capture.cap.release()
            cv2.destroyAllWindows()
            print("destroyAllWindows while")
            pass
```

### 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 any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)