c++ - 似乎无法正确关闭应用程序
问题描述
我一直在我的 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 关闭之外什么也不做,或者它给出了一个致命的异常。我在这里缺少什么来干净地关闭应用程序吗?
解决方案
推荐阅读
- swiftui - UIViewControllerRepresentable 奇怪的释放
- windows - 重命名文件夹后如何使用 Perforce P4V 客户端完全删除文件夹?
- ios - 如何为 iOS 14 设置 SwiftUI 应用程序,但如果 iOS 13 仍默认为 AppDelegate?
- hazelcast - IMap.lock() 是否需要 CP 子系统?
- javascript - Javascript 有选择地过滤响应
- html - 如果父容器是块级的,为什么父容器的行高会被其子元素的行高覆盖,但如果它是内联的则不会?
- fortran - Fortran中的增量值?
- python - 使用 min() 函数时忽略字符串
- python-3.x - Python3迁移xml写入问题
- node.js - 将方法重构为单独的文件时,Node.js找不到模块