title: OpenCV - imshow hangs if called two times from a thread
body: <p>everybody.</p>
  
  <p>I'm trying to have a separate thread to display and process images coming from a webcam.
  Those operations cannot be in the main thread, since it's dedicated to other tasks.
  What I need to do is to stop and eventually restart the thread.</p>
  
  <p>What happens is that the thread works the very first time, but at the second run the imshow call freezes forever.</p>
  
  <p>I reproduced the problew with a simple example deviated from another SO question (<a href="https://stackoverflow.com/questions/35189841/cvimshow-does-not-display-cvmat-color-when-on-different-thread">cv::imshow does not display cv::mat color when on different thread</a>)</p>
  
  <p><strong>non working multithread code</strong></p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  class Capture {
  private:
      bool running;
  
      std::thread thread;
      cv::Mat background;
      void loop() {
  
          while (running) {
              cv::imshow(windowName, background);  // at the second time the thread is started this instruction will hang
              cv::waitKey(500);
              Scalar color(rand()&amp;255, rand()&amp;255, rand()&amp;255);
              background.setTo(color);
          }
       cv::destroyWindow(windowName);
      cv::waitKey(1);
      }
  public:
      char windowName[128];
      Capture() :
      windowName{"test"},
      running{ false },
          thread{},
          background{ 800, 800, CV_8UC3, cv::Scalar{ 255, 0, 255 } } {
      }
      inline ~Capture() {
          if (running) stop(); // stop and join the thread
      }
      void run() {
          if (!running) {
              running = true;
              thread = std::thread{ &amp;Capture::loop, this };
          }
      }
      inline void join() { if (thread.joinable()) thread.join(); };
      inline void stop() {
          running = false;
          if (thread.joinable()) {
              thread.join();
          }
      }
  };
  
  int main()
  {
      Capture cap;
      // run the thread one time
      cap.run();
      std::this_thread::sleep_for(std::chrono::milliseconds(2500));
      cap.stop();
      // wait
      std::this_thread::sleep_for(std::chrono::milliseconds(500));
      // run again
      cap.run(); //stuck!!!
      std::this_thread::sleep_for(std::chrono::milliseconds(2500));
      cap.stop();
  
      return 0;
  }
  </code></pre>
  
  <p><strong>working singlethread code</strong></p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;thread&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  int main()
  {
      Scalar color(rand()&amp;255, rand()&amp;255, rand()&amp;255);
      cv::Mat background(800, 800, CV_8UC3, cv::Scalar{ 255, 0, 255 }) ;
      background.setTo(color);
      // try the first time to display an image
      for (int i=0; i&lt;5; i++)
      {
      cv::imshow("test", background);
          cv::waitKey(500);
          color=(rand()&amp;255, rand()&amp;255, rand()&amp;255);
          background.setTo(color);
      }
      // destroy the image
      cv::destroyWindow("test");
  
      // repeat the same as before
      for (int i=0; i&lt;5; i++)
      {
      cv::imshow("test", background);
          cv::waitKey(500);
          color=(rand()&amp;255, rand()&amp;255, rand()&amp;255);
          background.setTo(color);
      }
      cv::destroyWindow("test");
      // it worked !
      return 0;
  }
  </code></pre>
  
  <p>is there any reason why snippet #1 should not be working?</p>
  
  <p>Thanks, </p>
  
  <p>Marco</p>
  
  <p><strong>edit</strong></p>
  
  <p>It seems that both the snippets work using opencv3.x from the repository.
  It fails with opencv4.x compiled from scratch on Xubuntu 19.04.</p>