C++编程思想 第2卷 第11章 并发 使用线程 创建有响应的用户界面

使用线程处理的动机之一就是创建有响应的用户界面

从一个文件按行读取数据并把它们打印到控制台上
每行显示完成之后会休眠 sleeping 挂起 暂停执行当前线程 一秒钟
这个过程不会检查用户输入
所以用户界面是无响应的

//: C11:UnresponsiveUI.cpp {RunByHand}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Lack of threading produces an unresponsive UI.
//{L} ZThread
#include <iostream>
#include <fstream>
#include <string>
#include "zthread/Thread.h"
using namespace std;
using namespace ZThread;

int main() {
  cout << "Press <Enter> to quit:" << endl;
  ifstream file("UnresponsiveUI.cpp");
  string line;
  while(getline(file, line)) {
    cout << line << endl;
    Thread::sleep(1000); // Time in milliseconds
  }
  // Read input from the console
  cin.get();
  cout << "Shutting down..." << endl;
  getchar();
} ///:~

输出 1秒显示一行
ThreadQueue created
Press <Enter> to quit:
//: C11:UnresponsiveUI.cpp {RunByHand}
Reference thread created.
1 reference-thread added.
// From “Thinking in C++, Volume 2”, by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file ‘License.txt’,
// distributed with the code package available at www.MindView.net.
// Lack of threading produces an unresponsive UI.
//{L} ZThread
#include <iostream>
#include <fstream>
#include <string>
#include “zthread/Thread.h”
using namespace std;
using namespace ZThread;

int main() {
  cout << “Press <Enter> to quit:” << endl;
  ifstream file(“UnresponsiveUI.cpp”);
  string line;
  while(getline(file, line)) {
    cout << line << endl;
    Thread::sleep(1000); // Time in milliseconds
  }
  // Read input from the console
  cin.get();
  cout << “Shutting down…” << endl;
  getchar();
} ///:~

Shutting down…

为了使程序能够做出响应
可以在一个单独的线程中执行一个显示文件的任务
然后 主线程可以监视用户输入
程序就变成有响应的

//: C11:ResponsiveUI.cpp {RunByHand}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Threading for a responsive user interface.
//{L} ZThread
#include <iostream>
#include <fstream>
#include <string>
#include "zthread/Thread.h"
using namespace ZThread;
using namespace std;

class DisplayTask : public Runnable {
  ifstream in;
  string line;
  bool quitFlag;
public:
  DisplayTask(const string& file) : quitFlag(false) {
    in.open(file.c_str());
  }
  ~DisplayTask() { in.close(); }
  void run() {
    while(getline(in, line) && !quitFlag) {
      cout << line << endl;
      Thread::sleep(1000);
    }
  }
  void quit() { quitFlag = true; }
};

int main() {
  try {
    cout << "Press <Enter> to quit:" << endl;
    DisplayTask* dt = new DisplayTask("ResponsiveUI.cpp");
    Thread t(dt);
    cin.get();
    dt->quit();
  } catch(Synchronization_Exception& e) {
    cerr << e.what() << endl;
  }
  cout << "Shutting down..." << endl;
  getchar();
} ///:~

输出 要把源文件放到工程目录

ThreadQueue created
Press <Enter> to quit:
User thread created.
Reference thread created.
1 reference-thread added.
pollPendingThreads()
1 user-thread added.
Thread starting…
//: C11:ResponsiveUI.cpp {RunByHand}
// From “Thinking in C++, Volume 2”, by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file ‘License.txt’,
// distributed with the code package available at www.MindView.net.
// Threading for a responsive user interface.
//{L} ZThread
#include <iostream>
#include <fstream>
#include <string>
#include “zthread/Thread.h”
using namespace ZThread;
using namespace std;

class DisplayTask : public Runnable {
  ifstream in;
  string line;
  bool quitFlag;
public:
  DisplayTask(const string& file) : quitFlag(false) {
    in.open(file.c_str());
  }
  ~DisplayTask() { in.close(); }
  void run() {
    while(getline(in, line) && !quitFlag) {
      cout << line << endl;
      Thread::sleep(1000);
    }
  }
  void quit() { quitFlag = true; }
};

int main() {
  try {
    cout << “Press <Enter> to quit:” << endl;
    DisplayTask* dt = new DisplayTask(“ResponsiveUI.cpp”);
    Thread t(dt);
    cin.get();
    dt->quit();
  } catch(Synchronization_Exception& e) {
    cerr << e.what() << endl;
  }
  cout << “Shutting down…” << endl;
  getchar();

Shutting down…
Thread joining…
Thread exiting…
insertPendingThread()
1 pending-thread added.

mian()函数线程可以按下回车 Return 键立即做出响应
调用DisplayTask类的quit函数

    原文作者:eyetired
    原文地址: https://blog.csdn.net/eyetired/article/details/82626190
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞