此处先等30s,再调用函数ManagerTaskCore
类的定义:
#pragma once
#include <string>
#include <list>
#include <mutex>
#include <condition_variable>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/asio/steady_timer.hpp>
#include "../common.h"
#include "utils/thrd_list.hpp"
class TaskManager
{
public:
TaskManager(){ManagerTask();}
void ManagerTask();
void ManagerTaskCore();
int GeEarlyCidToStop();
void GeEarlyCidToStart();
void ManagerTime();
private:
std::string m_start_cid;
std::string m_stop_cid;
boost::asio::io_service m_io_ctx;
bool m_is_runnimg = false;
};
类的实现:
#include "task_manager.hpp"
void TaskManager::ManagerTask()
{
if(!m_is_runnimg)
{
m_is_runnimg = true;
}
else
{
return;
}
ManagerTime();
}
void TaskManager::ManagerTime()
{
std::cout << "dingshi start_____________________________________________!!!!! !"<< std::endl;
boost::asio::steady_timer asio_steady_timer(m_io_ctx);
asio_steady_timer.expires_from_now(std::chrono::seconds(g_opt.loop_time));
asio_steady_timer.async_wait([this](const boost::system::error_code& e){
ManagerTaskCore();
});
m_io_ctx.run();
}
void TaskManager::ManagerTaskCore()
{
std::cout << "loop start_____________________________________________!!!!! !"<< std::endl;
int start_count = 0;
int stop_count = 0;
for(auto it = g_CamInfoList.begin();it != g_CamInfoList.end(); it++)
{
if(it->isStop() == false)
{
start_count++;
}
if(it->isStop() == true && it->m_command == 1)
{
stop_count++;
}
}
if(start_count < g_opt.max_cam_num && stop_count > 0)
{
GeEarlyCidToStart();
}
if(start_count == g_opt.max_cam_num && stop_count > 0)
{
int a = GeEarlyCidToStop();
if(a == 0)
{
GeEarlyCidToStart();
}
}
// if(start_count == g_opt.max_cam_num && stop_count == 0)
// {
// return;
// }
ManagerTime();
}
int TaskManager::GeEarlyCidToStop()
{
std::time_t early_tp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
auto early_ptr = g_CamInfoList.end();
for(auto it = g_CamInfoList.begin();it != g_CamInfoList.end(); it++)
{
if(it->isStop() == false && it->m_loop == true)
{
if(it->m_tp < early_tp)
{
early_tp = it->m_tp;
early_ptr = it;
}
}
}
if(early_ptr != g_CamInfoList.end())
{
early_ptr->wait4Stop();
std::time_t tp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
early_ptr->m_tp =tp;
return 0;
}
else
{
return 1;
}
}
void TaskManager::GeEarlyCidToStart()
{
std::time_t early_tp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
auto early_ptr = g_CamInfoList.end();
for(auto it = g_CamInfoList.begin();it != g_CamInfoList.end(); it++)
{
if(it->isStop() == true)
{
if(it->m_tp < early_tp)
{
early_tp = it->m_tp;
early_ptr = it;
}
}
}
early_ptr->startDetect();
std::time_t tp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
early_ptr->m_tp =tp;
}
在main函数处初始化直接构造调用
std::shared_ptr<TaskManager> task_ptr = std::make_shared<TaskManager>();