c++ sleep_for比sleep时间精准

sleep(1)这种真实环境中会被打断,sleep不到1s

#include <iostream>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <thread>

#define HTTPCOUNT 1

int main()
{
    // 设置拉取http数据的标志
    // user_mode, 0 未知状态,保持上一次,1 开启,2 关闭
    // Wiper 1、2 开启,0、7 关闭
    static uint32_t cont_for_http = 0;
    static int mode_first = 0;
    static int mode_second = 0;
    static int wiprsts_first = 0;
    static int wiprsts_second = 0;
    static bool send_flag = false;
    while (1)
    {
        std::ifstream file("wiprsts.txt"); // 打开文件

        if (!file.is_open())
        {
            std::cerr << "Error opening file." << std::endl;
            return 1;
        }

        std::stringstream buffer0;
        buffer0 << file.rdbuf(); // 读取文件内容到缓冲区

        std::string content = buffer0.str();         // 将缓冲区内容转换为字符串
        // std::cout << content << std::endl;
        wiprsts_first = std::stoi(content.c_str()); // Convert string to int

        // // mode_first = (int)HttpsClient::get_instance()->response_json["data"]["user_mode"];
        // double Longitude = static_cast<double>(CANMsgRecv.GPSModuleSignals_Out.VINP_GPSLong_u32) / 100000;
        // double Latitude = static_cast<double>(CANMsgRecv.GPSModuleSignals_Out.VINP_GPSLat_u32) / 100000;
        send_flag = false;

        // cont_for_http = 0;
        // 触发http请求
        // HttpsClient::get_instance()->run(31.318467877460687, 121.24098583491161);
        // HttpsClient::get_instance()->run(Latitude, Longitude);
        // mode_first = (int)HttpsClient::get_instance()->response_json["data"]["user_mode"];
        std::ifstream file1("mode.txt"); // 打开文件
        if (!file1.is_open())
        {
            std::cerr << "Error opening file." << std::endl;
            return 1;
        }

        std::stringstream buffer;
        buffer << file1.rdbuf(); // 读取文件内容到缓冲区

        content = buffer.str(); // 将缓冲区内容转换为字符串
        // std::cout << content << std::endl;
        mode_first = std::stoi(content.c_str());

        if (mode_first == 2 && (wiprsts_first == 1 || wiprsts_first == 2) && (mode_second != 2 || (wiprsts_second != 1 && wiprsts_second != 2)))
        {
            cont_for_http = 0;
            send_flag = true;
        }

        if (mode_first == 1 && (wiprsts_first == 0 || wiprsts_first == 7) && (mode_second != 1 || (wiprsts_second != 0 && wiprsts_second != 7)))
        {
            cont_for_http = 0;
            send_flag = true;
        }

        wiprsts_second = wiprsts_first;
        mode_second = mode_first;

        if (cont_for_http == 20)
        {
            cont_for_http = 0;
            send_flag = true;
        }

        // todo send data
        if (send_flag)
        {
            std::cout << "send data send_flag ====: " << send_flag << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(5));
            std::cout << "send data send_flag ====: " << send_flag << std::endl;
        }
        else
        {
            std::cout << "send data send_flag: " << send_flag << std::endl;
        }

        std::cout << "wiprsts: " << (int)wiprsts_first << " mode: " << (int)mode_first << " cont_for_http: " << cont_for_http<< std::endl;
        cont_for_http ++;
        std::this_thread::sleep_for(std::chrono::seconds(HTTPCOUNT));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值