簡體   English   中英

調度問題 C++ windows

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM