【问题标题】:Running multiple functions at the same time? [closed]同时运行多个功能? [关闭]
【发布时间】:2013-05-06 15:48:20
【问题描述】:

这一定是一个被问过很多次的问题,但我找不到我要找的东西。

想象一下:

  • 一个程序启动“你好,你叫什么名字?”
  • 您输入了一个数字,然后显示“您的名字不能是数字!”

您不断输入一个数字并不断收到该错误,而在后台它只是通过每秒执行 n++ 来跟踪程序运行了多长时间,无论文本/输入部分发生什么。最终,您可以输入“时间”之类的内容,然后它会显示您在那里停留了多长时间,以秒为单位......

所以我的问题是:你到底要怎么做呢?让它们独立运行?

提前致谢!

编辑:我不是特别想做这个计时的事情,这只是我能想出的关于独立运行函数的最简单的例子..

【问题讨论】:

  • 阅读有关计时器和多线程的一般信息。
  • 有什么原因不能只检查开头和结尾的时间吗?
  • 如果我第一次输入正确的名字怎么办?
  • 你要找的东西叫做并发。

标签: c++ multithreading time


【解决方案1】:

我并不是特别想做这个计时的事情,这只是我能想出的关于独立运行函数的最简单的例子..

那么您可能想研究多线程。在 C++11 中,您可以这样做:

#include <thread>
#include <iostream>

void func1() {
    std::cout << "func1" << std::endl;
}

void func2() {
    std::cout << "func2" << std::endl;
}

int main() {
    std::thread td1(func1);
    std::thread td2(func2);
    std::cout << "Started 2 threads. Waiting for them to finish..." << std::endl;
    td1.join();
    td2.join();
    std::cout << "Threads finished." << std::endl;
    return 0;
}

如果您不使用 C++11,您仍然可以选择。您可以查看:

【讨论】:

  • 感谢您的回答——我现在只有一个问题; .join() 函数有什么作用?
  • 它等待一个线程完成并返回给调用者。
【解决方案2】:

如果你真的想使用线程,而不仅仅是计算时间,你可以使用boost

示例:

include <boost/thread.hpp>

void task1() { 
    // do something
}

void task2() { 
    // do something
}

void main () {
    using namespace boost; 
    thread thread1 = thread(task1);
    thread thread2 = thread(task2);
    thread2.join();
    thread1.join();
}

【讨论】:

    【解决方案3】:

    您无需运行并行任务即可测量经过的时间。 C++11 中的一个例子:

    #include <chrono>
    #include <string>
    #include <iostream>
    
    int main()
    {
        auto t1 = std::chrono::system_clock::now();
    
        std::string s;
        std::cin >> s;
        // Or whatever you want to do...
    
        auto t2 = std::chrono::system_clock::now();
        auto elapsedMS =
            (std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)).count()
    
        std::cout << elapsedMS;
    }
    

    编辑:

    由于您似乎对并行启动多个任务的方式感兴趣,这里有一个提示(再次使用 C++11):

    #include <ctime>
    #include <future>
    #include <thread>
    #include <iostream>
    
    int long_computation(int x, int y)
    {
        std::this_thread::sleep_for(std::chrono::seconds(5));
    
        return (x + y);
    }
    
    int main()
    {
        auto f = std::async(std::launch::async, long_computation, 42, 1729);
    
        // Do things in the meanwhile...
        std::string s;
        std::cin >> s;
        // And we could continue...
    
        std::cout << f.get(); // Here we join with the asynchronous operation
    }
    

    上面的例子开始了一个至少需要 5 秒的长时间计算,而 与此同时 会做其他事情。然后,最终,它在 future 对象上调用 get() 以加入异步计算并检索其结果(如果尚未完成,则等待它完成)。

    【讨论】:

    • 你可以在“错误循环”中显示代码,他可以检查是否输入了“时间”,然后相应地显示当前经过的时间。
    • @Koushik 查看 OP 的编辑,他对这个例子并不特别感兴趣。他在问多线程。
    • @JBentley 哎呀错过了。抱歉
    • @JBentley:确实,我是在 OP 编辑​​之后才意识到这一点的。我编辑了答案。
    • 好吧,我不想这么说,但我大约一个月前才开始学习 C++。在那之前我正在学习 Java,而且,嗯……与 Java 相比,C++ 相当复杂……也许我应该回去
    【解决方案4】:

    首先,您不需要增加自己的时间变量。只需记录程序启动的时间,time 命令会返回现在时间与开始时间的差。

    更一般地说 -

    1. 可以在另一个线程中启动一个长时间运行的任务。你需要自己研究这个;尝试在谷歌上搜索该短语。
    2. 事件驱动的编程可能更适合此用例。试试谷歌的“C++ 事件驱动 IO”之类的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多