你好!
我最近想知道C ++中的协程的状态,我发现了几个实现。我决定选择一个用于我的实验。
它简单易用,适用于Linux和Windows。

我的目标是试图找到一种方法来让代码异步运行,而不必等待信号触发插槽,并避免调用QCoreApplication :: processEvents或在堆栈中创建QEventLoop。

我的第一种方法是将自定义事件调度程序的processEvent函数转换为协程并使用yield。几次失败后,我决定不继续这种方式。

我的下一个尝试是将一个插槽转换为协程:

1
QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([]() { ... });

在这个lambda中,CPU将执行代码直到yield,它将跳回到应用程序事件循环。
完整的代码是:

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>
 
int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  QPushButton fibonacciButton("0: 0");
  fibonacciButton.show();
  QObject::connect(&fibonacciButton, &QPushButton::pressed,
                   std::bind(&coroutine::resume, coroutine::create([&]() {
    qulonglong f0 = 1, f1 = 0, n = 1;
    fibonacciButton.setText(QString("1: 1"));
    coroutine::yield();
    fibonacciButton.setText(QString("2: 1"));
    forever {
      auto next = f1 + f0;
      f0 = f1;
      f1 = next;
      fibonacciButton.setText(QString("%0: %1").arg(n++).arg(f0 + f1));
      coroutine::yield();
    }
  })));
  return app.exec();
}

在这里我们可以看到一个连接到一个lambda函数的按钮,它可以计算斐波那契数列中的数字。在计算下一个数字之后,我们调用yield,它将从该函数跳转到事件循环。当用户再次按下该按钮时,代码将在收益率之后返回到下一行。

这个例子的作用是因为用户需要再次按下按钮来恢复代码的执行。

但是,有时我们想要自动恢复执行。为此,我们需要产生执行并安排执行的简历:

1
2
3
4
6
void qYield()
{
  const auto routine = coroutine::current();
  QTimer::singleShot(0, std::bind(&coroutine::resume, routine));
  coroutine::yield();
}

第一行获取协程的标识符,第二行安排恢复。有了yield,CPU会回到以前的帧,最后到达主循环,并且无条件恢复代码。

下一步是在情况发生时尝试恢复。Qt提供了指示什么时候发生的信号,所以更优化的执行方式是:

1
2
3
4
6
7
8
9
template <typename Func>
void qAwait(const typename QtPrivate::FunctionPointer::Object *sender, Func signal)
{
  const auto routine = coroutine::current();
  const auto connection = QObject::connect(sender, signal,
                                           std::bind(&coroutine::resume, routine));
  coroutine::yield();
  QObject::disconnect(connection);
}

我们创建一个临时连接来恢复插槽的执行,而不是排队恢复。

一个例子可以是:

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>
 
int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  QPlainTextEdit textEdit;
  textEdit.show();
  QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([&]() {
    QUrl url("http://download.qt.io/online/qt5/linux/x64/online_repository/Updates.xml");
    QNetworkRequest request(url);
    QNetworkAccessManager manager;
    auto reply = manager.get(request);
    qAwait(reply, &QNetworkReply::finished);
    textEdit.setPlainText(reply->readAll());
    reply->deleteLater();
  })));
  return app.exec();
}

在这里,我创建了一个QTextEdit,它从互联网上接收文件的内容。当QNetworkReply完成时,数据被写入QTextEdit。

另一个例子:

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>
 
int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  QPlainTextEdit edit;
  edit.show();
 
  QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([&]() {
    auto previousText = edit.toPlainText();
    forever {
      if (edit.toPlainText() == QStringLiteral("quit")) {
        qApp->quit();
      } else if (previousText != edit.toPlainText()) {
        qDebug() << previousText << "->" << edit.toPlainText();
        previousText = edit.toPlainText();
      }
      qAwait(&edit, &QPlainTextEdit::textChanged);
      qDebug() << "QPlainTextEdit::textChanged";
    }
  })));
  return app.exec();
}

每次用户修改文本时,该应用程序都会打印文本,并在用户写入“退出”字时完成执行。

https://blog.qt.io/blog/2018/05/29/playing-coroutines-qt/

Playing with coroutines and Qt的更多相关文章

  1. Qt: The State Machine Framework 学习

    State Machine,即为状态机,是Qt中一项非常好的框架.State Machine包括State以及State间的Transition,构成状态和状态转移.通过状态机,我们可以很方便地实现很 ...

  2. qt Multimedia 模块类如何使用?

    qt 多媒体模块介绍 类名 英文描述 中文描述 QAudioBuffer Represents a collection of audio samples with a specific format ...

  3. Qt Multimedia 模块类如何使用?(表格)

    qt 多媒体模块介绍 类名 英文描述 中文描述 QAudioBuffer Represents a collection of audio samples with a specific format ...

  4. API Design Principles -- QT Project

    [the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...

  5. qtchooser - a wrapper used to select between Qt development binary(2种方法)

    ---------------------------------------------------------------------------------------------------- ...

  6. GStreamer基础教程11 - 与QT集成

    摘要 通常我们的播放引擎需要和GUI进行集成,在使用GStreamer时,GStreamre会负责媒体的播放及控制,GUI会负责处理用户的交互操作以及创建显示的窗口.本例中我们将结合QT介绍如何指定G ...

  7. 简单的多屏播放器示例(vlc+qt)

      介绍 简单的多屏播放器 最多同时播放16个视频 支持本地文件和rtsp.rtmp等流媒体播放 VS2015工程,依赖Qt+VLC 练手作品 截图 下载 程序:download.csdn.net/d ...

  8. vlc音视频开发(一)环境搭建(qt篇)

    来源:微信公众号「编程学习基地」 目录 简介 qt配置vlc环境 simple_libvlc_qt_player 项目地址 简介 VLC 是一款自由.开源的跨平台多媒体播放器及框架,可播放大多数多媒体 ...

  9. QT内省机制、自定义Model、数据库

    本文将介绍自定义Model过程中数据库数据源的获取方法,我使用过以下三种方式获取数据库数据源: 创建 存储对应数据库所有字段的 结构体,将结构体置于容器中返回,然后根据索引值(QModelIndex) ...

随机推荐

  1. 集群通信组件tribes之用法

    上面已经对tribes的内部实现机制及原理进行了深入的剖析.在理解它的设计原理后看看怎样使用tribes.整个使用相当简单便捷,仅仅须要四步: ① 定义一个消息对象,因为这个消息对象是要在网络之间传递 ...

  2. RvmTranslator6.0 - Dassault Systemes 3DXML

    RvmTranslator6.0 - Dassault Systemes 3DXML eryar@163.com 1. Introduction RvmTranslator can translate ...

  3. call() 和 apply() 的作用和区别

    call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就 ...

  4. $.each(data, function (index, value) { })的用法;json和list<>的互相转换

    在json中常常碰到这样的代码: jquery $.each(data, function (index, value) {    }) 遍历处理data,可以是数组.DOM.json等,取决于直接给 ...

  5. ZJOI2005沼泽鳄鱼

    矩阵优化dp ** 注意:矩阵乘法没有交换律 ** 思路:类比P2151hh去散步 代码特点在一维的答案矩阵 1.矩阵优化两点间方案数不必赘述 2.注意2,3,4可以办到以他们的lcm为周期,正是因为 ...

  6. FreeModbus RTU slave & Modbus RTU master

    一.FreeModbus RTU 协议数据格式 FreeModbus RTU是开源的一个协议,并且使用FreeModbus RTU 只能当做从机Slave,RTU协议中的指令由地址码(一个字节),功能 ...

  7. WinRAR 5.40无弹窗广告注册版下载

    WinRAR 5.40无弹窗广告注册版下载  资料来源  http://www.heminjie.com/network/6366.html WinRAR 5.40 下载安装后,打开压缩包文件会弹出广 ...

  8. linux设置tab键的宽度为4

    先cd 到~目录 ~$ cd ~$ vi .vimrc set nu

  9. 用C#生成随机中文汉字验证码的基本原理

    前几天去申请免费QQ号码,突然发现申请表单中的验证码内容换成了中文,这叫真叫我大跌眼镜感到好笑,Moper上的猫儿们都大骂腾讯采用中文验证码.^_^  我不得不佩服腾讯为了防止目前网络上横行的QQ号码 ...

  10. JAVA MessageDigest(MD5加密等)

    转自http://blog.csdn.net/hudashi/article/details/8394158 一.概述 java.security.MessageDigest类用于为应用程序提供信息摘 ...