首页 > 解决方案 > 似乎无法正确关闭应用程序

问题描述

我一直在我的 main() 中使用这段代码

int go3 = 0;
std::atomic<bool> is_done{ false };

int camera() {
    const int WIDTH = 640;
    const int HEIGHT = 480;
    cv::VideoCapture cap(1);
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
    cap.set(cv::CAP_PROP_FPS, 60);
    cv::Mat frame;
    cv::Mat srcImage = cv::imread("img/zoom log_50sm.png", -1);
    cv::Mat mask;
    vector<cv::Mat> rgbLayer;
    // First, create a virtual camera instance with scCreateCamera().
    // A virtual camera is a source of a video image stream.
    // The dimension width and height can be arbitrary positive numbers
    // which are multiples of four.
    // The third argument framerate is used to make sending frames at regular intervals.
    // This framerate argument can be omitted, and the default framerate is 60.
    // If you want to send every frame immediately without the frame rate regulator,
    // specify 0 to the framerate argument, then it will be a variable frame rate.
    scCamera cam = scCreateCamera(WIDTH, HEIGHT, 60);

    // Here, we wait for an application to connect to this camera.
    // You can comment out this line to start sending frames immediately
    // no matter there is a receiver or not.
    scWaitForConnection(cam);
    while (go3 != 1)
    {
        //Mat frame;
        cap.read(frame);
        cv::Mat result;

        //overlayImage(frame, srcImage, result, cv::Point(16, 80));
        //srcImage.copyTo(frame(cv::Rect(20, 75, srcImage.cols, srcImage.rows)), mask);
        // Draw bouncing balls.
        //balls.move(1.0f / 60.0f);
        //balls.draw(image.data());

        // Send the image as a newly captured frame of the camera.
        scSendFrame(cam, frame.ptr());
    }
    is_done = true;
    return 0;
}

int main() {
    thread thread2(camera);
    thread2.detach();
    MyApp app;
    app.Run();
    //thread2.join();
    go3 = 1;
    //thread2.join();
    if (is_done == true) {
        using namespace std::this_thread; // sleep_for, sleep_until
        using namespace std::chrono; // nanoseconds, system_clock, seconds
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return 0;
    }
}

问题是每次我尝试关闭程序时,窗口都会挂起并且除了线程 2 关闭之外什么也不做,或者它给出了一个致命的异常。我在这里缺少什么来干净地关闭应用程序吗?

标签: c++multithreadingopencv

解决方案


推荐阅读