【问题标题】:What's the right way to count the thread run time?计算线程运行时间的正确方法是什么?
【发布时间】:2021-01-25 00:00:42
【问题描述】:

像这样:

    thread t1(getPrimes,begin,end, ref(vect));
    start = chrono::high_resolution_clock::now();
    t1.join();
    finish = chrono::high_resolution_clock::now();

或者像这样:

    start = chrono::high_resolution_clock::now();
    thread t1(getPrimes,begin,end, ref(vect));
    t1.join();
    finish = chrono::high_resolution_clock::now();

我应该在线程 t1 之前还是之后启动它?

【问题讨论】:

    标签: c++ multithreading runtime


    【解决方案1】:

    新执行线程的开始随着std::thread的构造函数的完成而排序。

    在第一个版本中,整个执行线程理论上可以在原执行线程之前运行并完成 第一次调用now()。 C++ 不保证执行线程仅在第一次调用 now 之后才会真正启动和运行。

    第二个版本保证覆盖了整个执行线程,加上std::thread的构造的一点点开销。

    将运行时捕获移动到实际执行线程本身会更好。

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多