完善部分算法线程

This commit is contained in:
2024-05-24 00:55:08 +08:00
parent eef1ee54b1
commit 16f0d4f4c8
4 changed files with 44 additions and 72 deletions
@@ -450,7 +450,7 @@ void calculateLineSegmentDistances(const std::vector<std::pair<cv::Point, cv::Po
double distance = calculateLineSegmentDistance(line1, line2);
if (distance >= 0) {
sumDistance += distance;
std::cout << "Minimum distansssdce: " << distance * 0.014744 << std::endl;
std::cout << "Minimum dist ssdce: " << distance * 0.014744 << std::endl;
if (distance < minDistance) {
minDistance = distance;
}
@@ -43,42 +43,6 @@ void camera::run()
std::vector<cv::Mat>camera_mat;
while (runFlag)
{
//getCameraImg(camera_mat);
//while(camera_mat.size()>0)
//{
// int size = imgQueue.size();
// for (auto img : camera_mat)
// {
// imgQueue.enqueue(img);
// if (lastUpdateTime.elapsed() > 200)//显示在界面
// {
// lastUpdateTime.start(); // 记录本次更新时间
// QPixmap pixmap;
// cvMatToQPixMap(img, pixmap);
// showImgPack* pack = new showImgPack();
// pack->img = pixmap;
// pack->type = imgWidgetOri;
// pack->index = index;
// emit S1(pack);
// }
// else
// {
// showLost++;
// qDebug() << cameraModulePtr.moduleName << "Lost show";
// }
// camera_mat.clear();
// }
// //防止过大
// while (imgQueue.size() > 5)
// {
// imgQueue.dequeue();
// imgLost++;
// qDebug() << cameraModulePtr.moduleName << "Lost img";
// }
//
// getCameraImg(camera_mat);
//}
int queueSize = requestQueue.size() ;
if (queueSize >0)
{
@@ -48,43 +48,51 @@ threadManager::threadManager(QObject* parent) : QThread(parent)
void threadManager::process()
{
#pragma region 检查队列是否有新的数据
queueMutex.lock();
while (insertQueue.size() > 0)
processData.push_back(std::make_pair(progress::waitImg,insertQueue.dequeue()));
waitImgData.push_back(insertQueue.dequeue());
queueMutex.unlock();
#pragma endregion
#pragma region 检测状态并处理
for (auto& data : processData)
#pragma region 等待图片满足要求后异步执行算法函数
for (auto it = waitImgData.begin(); it != waitImgData.end();)
{
std::shared_ptr <detData> detDataPtr = data.second;
switch (data.first)
std::shared_ptr<detData> ptr = *it;
if (ptr->imgs.size() >= ptr->imgAmountMin)
{
case waitImg:
if (detDataPtr->imgs.size() >= detDataPtr->imgAmountMin)
{
futures.insert(std::make_pair(detDataPtr->timeCode,std::async(std::launch::async, pinAioEvalute, detDataPtr)));
data.first = processing;
}
break;
case processing:
auto futrue = futures.find(detDataPtr->timeCode);
if (futrue == futures.end())
{
std::cout << "???????" << std::endl;
}
if (futrue->second.valid())
{
//processData
}
futures.insert(std::make_pair(ptr->timeCode, std::async(std::launch::async, pinAioEvalute, ptr)));
it = waitImgData.erase(it);
processData.push_back(ptr);
}
else
++it;
}
#pragma endregion
#pragma region 执行完毕后异步获取储存使用结果
for (auto it = processData.begin(); it != processData.end();)
{
std::shared_ptr<detData> ptr = *it;
futrueIte = futures.find(ptr->timeCode);
if (futrueIte->second.valid())
{
it = processData.erase(it); // 删除元素后,更新迭代器
futures.erase(futrueIte);
std::async(std::launch::async, &threadManager::resultHandle, this, ptr);
}
else
++it;
}
#pragma endregion
}
}
void threadManager::resultHandle(std::shared_ptr <detData>)
{
}
void threadManager::run()
{
while(1)
while (runFlag == true)
{
process();
QThread::msleep(1);
@@ -103,6 +111,11 @@ void threadManager::addTask(std::shared_ptr <detData> data)
}
threadManager::~threadManager()
{
runFlag = false;
this->stop();
this->terminate();
processData.clear();
futures.clear();
}
////////////////DET//DATA//////////////////
sncodeCls::sncodeCls()
@@ -218,20 +218,11 @@ public:
sncodeCls();
};
/// <summary>
/// 线程池管理
/// </summary>
class threadManager : public QThread
{
Q_OBJECT
public:
enum progress
{
waitImg,
processing
};
explicit threadManager(QObject* parent = nullptr);
QQueue<std::shared_ptr <detData>> detDataQueue;
sqlManager* sql;
@@ -246,6 +237,7 @@ public:
runFlag = false;
this->terminate();
}
void resultHandle(std::shared_ptr <detData>);
void addTask(std::shared_ptr <detData>);
sncodeCls snDatas;
@@ -255,10 +247,13 @@ public:
QMutex insertQueueMutex;
QQueue<std::shared_ptr <detData>>insertQueue;
std::vector<std::pair<progress,std::shared_ptr <detData>>> processData;
std::unordered_map<unsigned long long,std::future<void>> futures;
void process();
private:
std::list<std::shared_ptr <detData>> waitImgData;
std::list<std::shared_ptr <detData>> processData;
std::unordered_map<unsigned long long, std::future<void>> futures;
std::unordered_map<unsigned long long, std::future<void>>::iterator futrueIte;
bool runFlag = true;
QList<std::shared_ptr <detData>>imgWaitList;
std::vector<std::shared_ptr <detData>>deleteVec;