【发布时间】:2021-05-21 13:14:17
【问题描述】:
我正在编写一个从外部库接收帧日期的生产者/消费者代码。
每一帧都是从一个在并行线程中运行的外部库的回调函数中读取的,并被推送到一个 Mat 队列中。我创建了另一个在不同线程中运行的函数,该线程读取并弹出每一帧。
问题是我在尝试从队列中读取帧数据时收到“访问冲突读取位置”。
我在全局声明这些变量:
queue<Mat> matQ;
OnFrameDataReceivedCB videoCB;
OnDeviceConnectStatusCB connectCB;
guide_usb_video_mode_e videoMode;
int width = 640;
int height = 512;
std::mutex mu;
下面是推送每帧数据的回调函数代码:
void OnVideoCallBack(const guide_usb_frame_data_t data) //callback function
{
if (data.frame_rgb_data_length > 0)
{
// Send the displayed data directly
unsigned char* rgbData;
Mat frame;
Size size = Size(width, height);
rgbData = data.frame_rgb_data;
frame = Mat(size, CV_8UC3, rgbData, Mat::AUTO_STEP);
if (mu.try_lock())
{
printf("producing...\n");
matQ.push(frame);
printf("free producing\n");
mu.unlock();
}
}
}
这是从队列中读取的函数:
void OnHandleVideoData()
{
while (true)
{
try
{
if (matQ.size() <= 0)
{
chrono::milliseconds duration(200);
this_thread::sleep_for(duration);
continue;
}
if (mu.try_lock())
{
if (matQ.size() > 0)
{
printf("consuming...\n");
Size size = Size(width, height);
Mat frame = Mat(size, CV_8UC3);
frame = matQ.front().clone();
matQ.pop();
imwrite("frame.jpg", frame); //the access violation exception is thrown on this line
printf("free consuming\n");
mu.unlock();
}
}
}
catch (...)
{
}
}
}
我也尝试将 unsigned char* rgbData 数组放入队列而不是 Mat,但我得到了同样的错误。
我错过了什么?
【问题讨论】:
标签: c++ multithreading opencv queue mutex