简体   繁体   English

调度问题 C++ windows

[英]Schedular issue C++ windows

I have a scheduler code to perform some task as per given time.我有一个调度程序代码来按照给定的时间执行一些任务。 I have a table in database of name config , where different time is given to every row which have time in second to read data from a different table.我在名称为config的数据库中有一个表,其中为每一行赋予不同的时间,这些行有时间从不同的表中读取数据。 I gave time in the format like.我以类似的格式给出了时间。 20, 40, 50, 60 seconds. 20、40、50、60秒。

Code should run in such a way that when I start my program after every 20 second that column should execute which have 20 second, after every 40 second that column should execute which have 40 second and so on.代码应该以这样一种方式运行,即当我每 20 秒后启动程序时,该列应该执行 20 秒,每 40 秒后该列应该执行 40 秒,依此类推。 But it is not executing in that way.但它不是以这种方式执行的。 Scheduler is not working as per given seconds, it is skipping and mismatching time调度程序没有按给定的秒数工作,它正在跳过和不匹配的时间

Below is a code where I am trying to read data as per time given in config table下面是我试图按照配置表中给出的时间读取数据的代码

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();
    }
}

OS - Windows 10 64 bit Home操作系统 - Windows 10 64 位

Visual Studio - 15.9.19视觉工作室 - 15.9.19

Database - PostgreSQL数据库 - PostgreSQL

I think it maybe that if (++tick_count > this->scan_rate_max) causes that problem.我认为if (++tick_count > this->scan_rate_max)可能会导致该问题。 When ++tick_count , it leads to change tick_count from 1 to 2. So, tick_count in if (tick_count % scheduling_struct.scan_rate == 0) is bigger than expected.++tick_count时,它会导致 tick_count 从 1 变为 2。因此, if ( tick_count if (tick_count % scheduling_struct.scan_rate == 0)中的 tick_count 比预期的要大。 I suggest that you can try to change ++tick_count to 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM