调用函数有问题。 C++

Having problem with calling function. C++

我写了一个 SetInterval 和 SetTimeOut 函数作为 JavaScript 函数,但是我不能设置低 100 毫秒,我也不能调用两个 SetInterval。请帮助我!

包括

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
using namespace std::chrono;
using namespace std;

milliseconds ms = duration_cast<milliseconds>(
    system_clock::now().time_since_epoch()
    );

带参数函数的SetTimeOut

 template <typename Proc, typename T>
    void SetTimeOut(T & val, Proc p, unsigned long long n) {
        unsigned long long def = ms.count(); // starting point
        unsigned long long ds = ms.count() + n; // the time what need become equal to def
    
        while (def != ds) {
            milliseconds ms = duration_cast<milliseconds>(
                system_clock::now().time_since_epoch()
                ); // time updates
            def = ms.count(); // converting to nums
    
        }
        p(val); // calling the main function
    }

相同,但调用无参数函数

template <typename Proc>
void SetTimeOut(Proc p, unsigned long long n) {
    unsigned long long def = ms.count() ;
    unsigned long long ds = ms.count() + n;
    while (def != ds) {
        milliseconds ms = duration_cast<milliseconds>(
            system_clock::now().time_since_epoch()
            ); // time updates
        def = ms.count();
    }
    p(); // Main function calling
}

p - 我们要调用的函数,n - 应该经过多少时间,s - 应该重复多少次。 SetInterval with unparameter function

template <typename Proc>
void SetInterval(Proc p, unsigned long long n, long long s) {
    //here there are call in turn of function SetTimeOut
    for (int i = 1; i != s + 1; i++) SetTimeOut(p, n * i);
}
//Same, but for parameter function
template <typename T, typename Proc>
void SetInterval(T val, Proc p, unsigned long long n, long long s) { 
    for(int i = 1; i != s+1; i++) SetTimeOut(val, p, n * i);

}
 //Example with struct
  struct Car{
    string model;
    double speed;
    int fuel;
};

主要功能

int main() {
    Car Car;
    int i = 1;
    Car.fuel = 200;
    Car.speed = 10.2;
    
    //Example with lambda function.
    SetInterval(Car,[&](struct Car){
        Car.speed += 1.9;
        Car.fuel -= 1.2;
        cout << Car.speed << " " << Car.fuel << endl;
        }, 900, 10); // it will end after 10 calls with 0.9 sec interval
     //Second Example, which is not working
    SetInterval(i, [&](int i){ i++; cout << i << endl;}, 500, 5);
}

我认为是因为调用时间交叉,但我不知道如何解决。

不要这样做:

    while (def != ds) {
        milliseconds ms = duration_cast<milliseconds>(
            system_clock::now().time_since_epoch()
            ); // time updates
        def = ms.count(); // converting to nums

    }
    p(val); // calling the main function

这是一个自旋循环。它只会挂住整个 CPU 核心,同时不断检查以查看 "is it time yet"。至少,在那里放一个睡眠语句并检查 <= 而不是 != 以防你的时钟提前。

    while (def <= ds) {

        duration<milliseconds> tosleepfor(ds-def);
        this_thread::sleep_for(tosleepfor);

        milliseconds ms = duration_cast<milliseconds>(
            system_clock::now().time_since_epoch()
            ); // time updates
        def = ms.count(); // converting to nums

    }
    p(val); // calling the main function

但这就是您设计的根本问题。 Javascript 具有用于执行异步工作的内置消息泵 - 包括使用回调设置计时器。 C++ 没有这样的设施。您的 SetTimeout 函数实际上并未设置超时。它只是暂停程序,直到时间到了。

您必须利用 OS 的 GUI 线程或拥有自己的消息泵送概念以按特定时间间隔调度回调。