[英]C++ : How do I execute a function after several seconds?
Firstly, I'm using VS2008 (doesn't support C++11). 首先,我使用的是VS2008(不支持C ++ 11)。 I can't upgrade and need to use native libraries only because it needs to be compiled on another persons' compiler which I don't have control over.
我无法升级并且只需要使用本机库,因为它需要在我无法控制的其他人的编译器上编译。
I would like to run the code automatically after 5 seconds without having to poll how many seconds have elapsed. 我想在5秒后自动运行代码,而不必轮询已经过了多少秒。
This is my incomplete code 这是我不完整的代码
#include <windows.h>
#include <iostream>
void runMeAfterFiveSeconds(){
cout<<"I'm activated!"<<endl;
}
void main(){
while(1){
cout<<"hello there!"<<endl;
Sleep(2000);
}
}
Example output 示例输出
hello there!
hello there! //after 2 seconds
hello there! //after 4 seconds
I'm activated! //after 5 seconds
hello there! //after 6 seconds
hello there! //after 8 seconds
hello there! //after 10 seconds
I'm activated! //after 10 seconds
...
This example shows how to do it using a very simple scheduling algorithm. 此示例显示如何使用非常简单的调度算法执行此操作。 No spawning of additional threads is required.
不需要产生额外的线程。
#include <stdio.h>
#include <windows.h>
int main(int argc, char ** argv)
{
DWORD now = timeGetTime();
DWORD nextPrintHelloThereTime = now;
DWORD nextPrintImActivatedTime = now+5000;
while(1)
{
now = timeGetTime();
DWORD nextEventTime = (nextPrintHelloThereTime < nextPrintImActivatedTime) ? nextPrintHelloThereTime : nextPrintImActivatedTime;
DWORD millisecondsToSleep = nextEventTime-now;
Sleep(millisecondsToSleep);
now = timeGetTime();
if (now >= nextPrintHelloThereTime)
{
printf("hello there!\n");
nextPrintHelloThereTime += 2000;
}
if (now >= nextPrintImActivatedTime)
{
printf("I'm activated!\n");
nextPrintImActivatedTime += 5000;
}
}
}
It really depends on what code you want to execute and how you want it to be executed. 这实际上取决于您要执行的代码以及您希望如何执行它。 The very simple way of doing so would be creating a separate thread and
Sleep()
in it. 这样做的非常简单的方法是在其中创建一个单独的线程和
Sleep()
。 So, since you cannot upgrade from Visual Studio 2008 (which, if I remember correctly, does not support C++11), you have to use either native Windows threads or some library implementation like Boost.Thread. 因此,由于您无法从Visual Studio 2008升级(如果我没记错,不支持C ++ 11),您必须使用本机Windows线程或某些库实现(如Boost.Thread)。 To look up how to use Windows threads, see MSDN documentation on _beginthreadex() function .
要查找如何使用Windows线程,请参阅_beginthreadex()函数上的MSDN文档 。 A short tutorial about Boost.Thread can bee seen here .
关于Boost.Thread的简短教程可以在这里看到。
Quick examples of both, taken directly from the links I provided: 两者的快速示例,直接取自我提供的链接:
1) Windows threads: 1)Windows线程:
// crt_begthrdex.cpp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>
unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" );
while ( Counter < 1000000 )
Counter++;
_endthreadex( 0 );
return 0;
}
int main()
{
HANDLE hThread;
unsigned threadID;
printf( "Creating second thread...\n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}
2) Boost.Thread: 2)Boost.Thread:
struct callable
{
void operator()();
};
boost::thread copies_are_safe()
{
callable x;
return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK
In the second example, you could as well have used a plain function pointer as boost::thread
constructor argument. 在第二个示例中,您还可以使用普通函数指针作为
boost::thread
构造函数参数。 Moreover, you could use a pointer to function with multiple arguments - a luxury Windows API's threads do not provide. 此外,您可以使用指向多个参数的指针 - 豪华Windows API的线程不提供。
You're probably just going to need to create a thread like so: 您可能只需要创建一个这样的线程:
#include <windows.h>
#include <iostream>
#include <thread>
void runMeAfterFiveSeconds(){
while(true){
sleep(5000);
cout<<"I'm activated!"<<endl;
}
}
void main(){
std::thread th(runMeAfterFiveSeconds);
while(1){
cout<<"hello there!"<<endl;
Sleep(2000);
}
}
You're going to have to either make a thread (Coding Orange's answer, probably the better way), or just write it all out. 你将不得不做一个线程(Coding Orange的答案,可能是更好的方法),或者只是写出来。
void runMeAfterFiveSeconds(){
cout << "I'm activated!" <<endl;
}
void main(){
while(1){
cout << "hello there!" << endl;
Sleep(2000);
cout << "hello there!" << endl;
Sleep(3000);
runMeAfterFiveSeconds();
Sleep(1000);
cout << "hello there!" << endl;
Sleep(2000);
cout << "hello there!" << endl;
Sleep(2000);
cout << "hello there!" << endl;
runMeAfterFiveSeconds();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.