如何在不停止应用程序的情况下延迟功能

时间:2015-09-18 03:34:21

标签: c++ c++11 visual-c++

void sendCommand(float t,char* cmd)
{

  std::clock_t endwait; 
  double endwait = clock () + t * CLOCKS_PER_SEC ; 
  while (clock() < endwait) {} 
  if( clock() < endwait)
  printf("\nThe waited command is =%s",cmd);

}

void Main()
{
 sendCommand(3.0,"Command1");
 sendCommand(2.0,"Command2");
 printf("\nThe first value")
 return 0;
}

我想延迟一个功能,但我的应用程序应继续运行。

在上面的代码中,我希望首先打印第一个值。 比我想要打印Command2并且Command1应该是最后打印的。

4 个答案:

答案 0 :(得分:4)

我更喜欢std::async

#include <chrono>
#include <thread>
#include <future>
#include <iostream>

void sendCommand(std::chrono::seconds delay, std::string cmd)
{
     std::this_thread::sleep_for( delay );
     std::cout << "\nThe waited command is =" << cmd;
}

int main()
{
 auto s1 = std::async(std::launch::async, sendCommand, std::chrono::seconds(3),"Command1");
 auto s2 = std::async(std::launch::async, sendCommand, std::chrono::seconds(2),"Command2");

 std::cout << "\nThe first value" << std::flush;    
 s1.wait();
 s2.wait();


 return 0;
}

但是,对于实际设计,我将创建一个调度程序(或最好使用现有的调度程序)来管理按延迟时间排序的优先级队列。为每个命令生成一个新线程很快就会成为一个问题。由于您标记了MS VIsual C ++的问题,请查看the PPL which implements task-based parallelism

因为这是一个C ++问题,我会远离C语言而不使用printfCLOCK_PER_SECchar*clock等等。你会很快得到当你开始使用字符串而不是“Command1”文字时,即使使用这个简单的例子也会遇到问题。 std::string会在这里帮到你。

答案 1 :(得分:2)

我认为你需要线程。你可以这样做:

#include <thread>
#include <chrono>
#include <iostream>

void sendCommand(float t, char const* cmd)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(int(t * 1000)));
    std::cout << "\nThe waited command is = " << cmd << '\n';
}

int main()
{
    // each function call on a new thread
    std::thread t1(sendCommand, 3.0, "Command1");
    std::thread t2(sendCommand, 2.0, "Command2");

    // join the threads so we don't exit before they finish.
    t1.join();
    t2.join();
}

答案 2 :(得分:0)

您可以根据实际逻辑以多种方式执行此操作。 Examles;:

1.你可以使用全局标志变量并检查其状态,当第三次打印完成后,你可以将标志设置为1,这样下一次调用就会执行。

2.你可以使用STACK .push STACK中的所有函数指针。之后弹出并执行。

3.MultiThreading。您可以使用适当的同步方式使用它,但它会很复杂。这取决于你的要求。

谢谢!!!

答案 3 :(得分:0)

boost::asio很好,因为它不需要多线程的开销。

#include <iostream>
#include <boost/asio.hpp>

using namespace std;

int main()
{
    boost::asio::io_service svc;

    boost::asio::deadline_timer t0{svc};
    boost::asio::deadline_timer t1{svc};
    boost::asio::deadline_timer t2{svc};

    t0.expires_from_now(boost::posix_time::seconds{1});
    t1.expires_from_now(boost::posix_time::seconds{2});
    t2.expires_from_now(boost::posix_time::seconds{3});

    t2.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t2!\n";});
    t1.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t1!\n";});
    t0.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t0!\n";});

    svc.post([] () { std::cout << "I'm number one!\n";});

    svc.run();

    return 0;
}

给出输出:

I'm number one!
Greetings from t0!
Greetings from t1!
Greetings from t2!