c++多线程队列
【摘要】 多线程中,使用队列不需要锁也能运行,取得时候先判断一下长度,长度为0线Sleep(3)。
struct RevData{ int size; char data[1000]; } recv_data; #include <iostream> #include <queue> #include<m...
多线程中,使用队列不需要锁也能运行,取得时候先判断一下长度,长度为0线Sleep(3)。
-
-
struct RevData{
-
-
int size;
-
-
char data[1000];
-
-
} recv_data;
-
-
-
#include <iostream>
-
-
#include <queue>
-
-
#include<map>
-
-
#include <string>
-
-
#include <mutex>
-
-
//#include <Windows.h>
-
-
-
-
using namespace std;
-
-
queue<RevData*> queue1;
-
-
queue<RevData*>* queue2;
-
-
//queue2 = (queue<RevData*>*)malloc(sizeof(queue<RevData*>));
-
-
-
-
for (int i = 0; i < 10; i++) {
-
-
RevData recvD;
-
-
recvD.size = i;
-
-
strcpy(recvD.data, "hello");
-
-
RevData* recvp = (RevData*)malloc(sizeof(RevData));
-
-
memcpy(recvp, &recvD, sizeof(RevData));
-
-
queue1.push(recvp);// recvp);
-
-
//queue1.push(&recvD);
-
-
}
-
-
-
-
queue2 = &queue1;
-
-
for (int i = 0; i < 10; i++) {
-
-
RevData* recv = queue2->front();
-
-
cout << (*recv).data << endl;
-
-
-
-
int lenga = queue2->size();
-
-
queue2->pop();
-
-
int lenga2 = queue2->size();
-
-
cout << (*recv).size << endl;
-
-
}
例子:
struct RevData {
int cam_id;
cv::Mat image;
} recv_data;
线程调用:
thread video_thread1(bind(&play_url, v_url));
thread video_thread2(bind(&detect_thread));
video_thread1.join();
int play_url(char* url){
}
get队列:
-
while (1) {
-
cv::Mat origin_image;//定义一个变量把视频源一帧一帧显示
-
//capture >> origin_image;
-
//if (origin_image.empty())
-
//{
-
// std::cout << "no video frame" << std::endl;
-
// break;
-
//}
-
-
bool is_get = FALSE;
-
//EnterCriticalSection(&g_cs);
-
if (queue1.size() > 0) {
-
RevData recv = queue1.front();
-
queue1.pop();
-
is_get = TRUE;
-
origin_image = (recv).image;
-
}
-
else {
-
//Sleep(3);
-
continue;
-
}
c++ 入队列:
-
start_time = GetTickCount();
-
-
sws_scale(img_convert_ctx, pFrameYUV->data, pFrameYUV->linesize, 0, avCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
-
-
cv::Mat mat(avCodecCtx->height, avCodecCtx->width, CV_8UC3, pFrameRGB->data[0], pFrameRGB->linesize[0]);
-
cv::imshow("", mat);//图像显示
-
cv::waitKey(1);
-
-
RevData recvD;
-
frame_index++;
-
recvD.cam_id = i;
-
-
recvD.image = mat.clone();
-
-
//RevData* recvp = (RevData*)malloc(sizeof(RevData));
-
-
//memcpy(recvp, &mat, sizeof(RevData));
-
-
//EnterCriticalSection(&g_cs);
-
if (frame_index%2==0) {
-
queue1.push(recvD);
-
}
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/89893403
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)