【问题标题】:C++ using proccesing power/time while console loading barC++ 在控制台加载栏时使用处理能力/时间
【发布时间】:2018-06-24 07:38:10
【问题描述】:

我目前是编程新手,不知道如何在加载栏运行时让程序处理。 我想显示加载栏并检查程序是否已完成处理。如果没有,那么处理栏应该重新开始填充。

我的代码现在只显示一次加载栏,然后开始处理。这没什么用。

我在 Google 和这里​​研究了堆栈溢出,但我只得到了加载栏,而不是我的程序中有用的集成。

我只需要一个简单的方法来检查,除了加载栏之外是否还有输出,我需要在加载栏的同时运行程序的其余部分,只是为了节省时间。

#include <iostream>
#include <windows.h>
#include <unistd.h>
#include <string>
#include <thread>

using namespace std;

static void loading(){
  system("Color 0A");
  cout<<"\t\n\n";
  char a=177, b=219;
  cout<<"\t";
  for (int i=0;i<=50;i++)
  cout<<a;
  cout<<"\r";
  cout<<"\t";
  for (int i=0;i<=50;i++){
   cout<<b;
   for (int j=0;j<=2e7;j++);
  }
  cout << "\n\n";
}

int main(){

//ProjectEuler Problem___

  loading();

  int j=0;
  do{
    j++;
  }while(j<=1e9); //just an example to see when it is processing

  cout << "hi" << endl;

  return 0;
}

感谢您的帮助。

【问题讨论】:

    标签: c++ progress-bar loading


    【解决方案1】:

    您正在寻找的典型模式如下所示:

    void do_expensive_work(std::atomic<bool>& done) {
      ... something expensive goes here...
      done = true;
    }
    
    int main() {
      std::atomic<bool> done = false;
      auto t = std::thread(do_expensive_work, std::ref(done));
      while (!done) {
        ... update your progress bar ...
        ... sleep a bit ...
      }
      t.join();
      return 0;
    }
    

    如何将计算结果传回调用线程等的细节取决于您。同样,理论上您可以使用全局原子来表示完成状态,或者将计算包装在一个跟踪状态并管理线程执行的类中。上面的代码被大大简化了,但给出了一个通用的模式。

    【讨论】:

      猜你喜欢
      • 2019-03-15
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多