相当于 C++ 的 window.setTimeout()
在 javascript 中,有一个非常非常好的函数 window.setTimeout( func, 1000 ) ;
,它将在 1000 毫秒后异步调用 func
。
我想在 C++ 中做类似的事情(没有多线程),所以我组合了一个示例循环,例如:
#include <stdio.h> struct Callback { // The _time_ this function will be executed. double execTime ; // The function to execute after execTime has passed void* func ; } ; // Sample function to execute void go() { puts( "GO" ) ; } // Global program-wide sense of time double time ; int main() { // start the timer time = 0 ; // Make a sample callback Callback c1 ; c1.execTime = 10000 ; c1.func = go ; while( 1 ) { // its time to execute it if( time > c1.execTime ) { c1.func ; // !! doesn't work! } time++; } }
我怎样才能使这样的事情工作?
In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ;
which will asynchronously invoke func
after 1000 ms.
I want to do something similar in C++ (without multithreading), so I put together a sample loop like:
#include <stdio.h> struct Callback { // The _time_ this function will be executed. double execTime ; // The function to execute after execTime has passed void* func ; } ; // Sample function to execute void go() { puts( "GO" ) ; } // Global program-wide sense of time double time ; int main() { // start the timer time = 0 ; // Make a sample callback Callback c1 ; c1.execTime = 10000 ; c1.func = go ; while( 1 ) { // its time to execute it if( time > c1.execTime ) { c1.func ; // !! doesn't work! } time++; } }
How can I make something like this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C++11出来后,如果你使用的是c++11支持的编译器,你可以使用lambda、可变参数模板函数和异步线程 strong> 轻松地用 C++ 模拟 javascript 函数。
这是我为 setTimeOut 编写的代码,它经过了充分测试:
setTimeOut 函数的定义:
此函数通过使用 c+11 的可变参数模板接受变量参数,
代码可以告诉你如何使用它:
After C++11 came out, and if your are using c++11 supported compiler, you can use lambda, variadic template function and asynchronous thread to simulate javascript function in c++ easily.
Here is the code I wrote for setTimeOut, it is fully tested:
setTimeOut funtion's definition:
This function accepts variable arguments by using c+11's variadic template,
The code can show you how to use it:
将
Callback::func
设为void (*)()
类型,即您可以这样调用该函数:
另外,不要忙等待。在 Linux 上使用
ualarm
或CreateWaitableTimer
。Make
Callback::func
of typevoid (*)()
, i.e.You can call the function this way:
Also, don't busy-wait. Use
ualarm
on Linux orCreateWaitableTimer
on Windows.就 C++ 本身而言,您的能力非常有限。您需要一个多线程操作系统,您可以在其中使用睡眠功能(您仍然可以让程序使用单独的线程继续运行),或者您需要一个消息系统,例如 Windows。
我认为你可以做某事的唯一另一种方法是在代码中分散大量调用,这些调用调用返回 true 或 false 的函数,具体取决于时间是否已过。当然,该函数可以是回调,但您仍然需要定期调用它。
In C++ by itself, you're pretty limited. You need either a multithreaded OS, where you could use a sleep function (you can still have the program continue running with a separate thread), or you need a messaging system, like Windows.
The only other way I see that you can do something is to have a lot of calls scattered throughout the code that calls a function that returns true or false, depending on whether the time has elapsed. The function could, of course, be a callback, but you would still need to call it periodically.
我只是在这里修复你的代码,而不是跳出框框思考。其他答案已经对此给出了一些指示。
为了更好的类型安全,您应该按如下方式声明回调指针:
然后,将其调用为:
I'm only fixing your code here, not thinking outside the box. The other answers already gave some pointers for that.
For better type safety, you should declare your callback pointer as follows:
Then, call it as:
另一个(更好的)答案是在 C++ 中使用
标头,并像这样声明函数:Another (better) answer would be to use the
<functional>
header in C++, and declare the function like so: