[英]Schedular issue C++ windows
我有一个调度程序代码来按照给定的时间执行一些任务。 我在名称为config的数据库中有一个表,其中为每一行赋予不同的时间,这些行有时间从不同的表中读取数据。 我以类似的格式给出了时间。 20、40、50、60秒。
代码应该以这样一种方式运行,即当我每 20 秒后启动程序时,该列应该执行 20 秒,每 40 秒后该列应该执行 40 秒,依此类推。 但它不是以这种方式执行的。 调度程序没有按给定的秒数工作,它正在跳过和不匹配的时间
下面是我试图按照配置表中给出的时间读取数据的代码
void plac::worker_thread(void)
{
try {
std::chrono::steady_clock::time_point tick_time = std::chrono::steady_clock::now();
std::uint32_t tick_count = 1;
plac::scheduling_struct scheduling_struct;
bool executing_backlog;
bool tick_iteration_complete = true;
std::uint32_t current_iteration;
while (1)
{
executing_backlog = false;
if (not this->scheduler_backlog.empty())
{
{
std::lock_guard<std::mutex> lck(this->mtx2);
current_iteration = this->scheduler_backlog.front();
this->scheduler_backlog.pop();
}
//executing_backlog = true;
}
if (not executing_backlog and tick_iteration_complete) {
//tick_time = std::chrono::steady_clock::now();
//std::this_thread::sleep_until(tick_time + std::chrono::milliseconds(1000)); //1 second delay
tick_time = std::chrono::steady_clock::now();
std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::milliseconds(1000)); //1 second delay
tick_iteration_complete = false;
if (++tick_count > this->scan_rate_max)
tick_count = 1;
}
{
std::lock_guard<std::mutex> lck(this->mtx1);
if (not executing_backlog)
{
current_iteration = this->scheduler_iteration;
if (++this->scheduler_iteration >= this->scheduler_list.size())
this->scheduler_iteration = 0;
}
scheduling_struct = this->scheduler_list.at(current_iteration);
}
if (tick_count % scheduling_struct.scan_rate == 0)
{
//std::cout << "hello" << std::endl;
if (this->_plac.find(scheduling_struct.ip) == this->_plac.end())
{
std::string error = fmt::format("No recored found in map at location {}", scheduling_struct.ip);
#ifdef _DEBUG
spdlog::error(error);
#endif // _DEBUG
LOG_ERROR << error;
continue;
}
if (not this->client.at(this->_plac.at(scheduling_struct.ip).client_location).read_progress)
{
const plac_common::config_struct config = this->_plac.at(scheduling_struct.ip).read_vector.at(scheduling_struct.config_serial_no);
if (not this->read_data(scheduling_struct))
{
spdlog::error("read data failed");
}
continue;
//this->read_data(current_iteration, client_location, config.area_type, config.area_number, config.read_location, config.read_length, config.word_length);
}
else
{
std::lock_guard<std::mutex> lck(this->mtx2);
this->scheduler_backlog.push(current_iteration);
}
}
tick_iteration_complete = true;
}
}
catch (const std::exception& ex) {
#ifdef _DEBUG
spdlog::error("Exception in worker thread. Exception : {}", ex.what());
#endif // _DEBUG
LOG_ERROR << ex.what();
}
}
操作系统 - Windows 10 64 位
视觉工作室 - 15.9.19
数据库 - PostgreSQL
我认为if (++tick_count > this->scan_rate_max)
可能会导致该问题。 当++tick_count
时,它会导致 tick_count 从 1 变为 2。因此, if ( tick_count
if (tick_count % scheduling_struct.scan_rate == 0)
中的 tick_count 比预期的要大。 我建议您可以尝试将++tick_count
更改为tick_count++
。
if (not executing_backlog and tick_iteration_complete) {
//tick_time = std::chrono::steady_clock::now();
//std::this_thread::sleep_until(tick_time + std::chrono::milliseconds(1000)); //1 second delay
tick_time = std::chrono::steady_clock::now();
std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::milliseconds(1000)); //1 second delay
tick_iteration_complete = false;
if (tick_count++ > this->scan_rate_max)
tick_count = 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.