【发布时间】:2017-07-27 20:53:31
【问题描述】:
在我尝试实现 multithreading 之前,我有一个运行良好的 Visual Studio 项目。该项目从GigE摄像头获取图像,获取10张图像后,将获取的图像制作成视频。
程序流程使得程序在制作视频时没有获取图像。我想改变这一点,所以我创建了另一个从图像制作视频的线程。我想要的是程序将连续获取图像,在获取 10 张图像后,另一个线程并行运行将制作视频。这将一直持续到我停止程序,获取 10 张图像,从这 10 张图像中并行制作视频,同时获取接下来的 10 张图像,依此类推。
我之前没有创建过threads,所以我按照this website 上的教程进行操作。与网站类似,我为保存视频的function 创建了一个thread。创建视频的 function 将 10 张图像作为 vector 参数。我在这个线程上执行join,就在我的main函数终止的那一行之前。
为了清楚起见,这里是我实现的伪代码:
#include ...
#include <thread>
using namespace std;
thread threads[1];
vector<Image> images;
void thread_method(vector<Image> & images){
// Save vector of images to video
// Clear the vector of images
}
int main(int argc, char* argv[]){
// Some stuff
while(TRUE)
{
for (int i = 0; i < 10; i++)
{
//Acquire Image
//Put image pointer in images vector named images
}
threads[0] = thread(thread_method, images)
}
// stuff
threads[0].join();
cout << endl << "Done! Press Enter to exit..." << endl;
getchar();
return 0;
}
当我现在运行项目时,会弹出一条消息说 Project.exe 已触发断点。该项目在report_runtime_error.cpp 中中断static bool __cdecl issue_debug_notification(wchar_t const* const message) throw()。
我正在控制台上打印一些cout 消息,以帮助我了解发生了什么。发生的情况是程序获取了 10 张图像,然后用于保存视频的 thread 开始运行。由于有 10 张图像,因此必须将 10 张图像附加到视频中。第二次获取10张图片后,弹出Project.exe已触发断点的消息,此时并行线程仅将第一次获取的一组图片中的6张图片附加到视频中。
输出包含多行thread XXXX has exited with code 0,之后输出显示
Debug Error!
Program: ...Path\Program.exe
abort() has been called
(Press Retry to debug the application)
Program.exe has triggered a breakpoint.
【问题讨论】:
-
@Ron 我已将
runStatus替换为TRUE,将result替换为0,并为images的定义添加了一行。Image是 API(相机 API)中的一个类,遗憾的是它不公开。
标签: c++ multithreading