Playing with coroutines and Qt
你好!
我最近想知道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的更多相关文章
- Qt: The State Machine Framework 学习
State Machine,即为状态机,是Qt中一项非常好的框架.State Machine包括State以及State间的Transition,构成状态和状态转移.通过状态机,我们可以很方便地实现很 ...
- qt Multimedia 模块类如何使用?
qt 多媒体模块介绍 类名 英文描述 中文描述 QAudioBuffer Represents a collection of audio samples with a specific format ...
- Qt Multimedia 模块类如何使用?(表格)
qt 多媒体模块介绍 类名 英文描述 中文描述 QAudioBuffer Represents a collection of audio samples with a specific format ...
- API Design Principles -- QT Project
[the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...
- qtchooser - a wrapper used to select between Qt development binary(2种方法)
---------------------------------------------------------------------------------------------------- ...
- GStreamer基础教程11 - 与QT集成
摘要 通常我们的播放引擎需要和GUI进行集成,在使用GStreamer时,GStreamre会负责媒体的播放及控制,GUI会负责处理用户的交互操作以及创建显示的窗口.本例中我们将结合QT介绍如何指定G ...
- 简单的多屏播放器示例(vlc+qt)
介绍 简单的多屏播放器 最多同时播放16个视频 支持本地文件和rtsp.rtmp等流媒体播放 VS2015工程,依赖Qt+VLC 练手作品 截图 下载 程序:download.csdn.net/d ...
- vlc音视频开发(一)环境搭建(qt篇)
来源:微信公众号「编程学习基地」 目录 简介 qt配置vlc环境 simple_libvlc_qt_player 项目地址 简介 VLC 是一款自由.开源的跨平台多媒体播放器及框架,可播放大多数多媒体 ...
- QT内省机制、自定义Model、数据库
本文将介绍自定义Model过程中数据库数据源的获取方法,我使用过以下三种方式获取数据库数据源: 创建 存储对应数据库所有字段的 结构体,将结构体置于容器中返回,然后根据索引值(QModelIndex) ...
随机推荐
- IOCP模型总结(总结回想)
IOCP旧代码重提.近期一直在玩其它方面的东东.时不时回想一下,收益多多. IOCP(I/O Completion Port,I/O完毕port)是性能最好的一种I/O模型.它是应用程序使用线程池处理 ...
- java 链接server上的 mongodb 出现 connect time out 问题
异常信息 十二月 22, 2014 5:27:58 下午 com.mongodb.DBTCPConnector initDirectConnection 警告: Exception executing ...
- Spark RPC
在Spark中,对于网络调用的底层封装(粘包拆包,编解码,链路管理等)都是在common/network-common包中实现的(详见[common/network-common]).在common/ ...
- Vue中独立组件之间数据交互
独立组件之间数据交互:通过自定义事件 组件A中的[数据],传递给组件B 1.创建组件A,组件B 2.组件B在实例创建完成时就开始监听事件[等待接收数据]:钩子 3.组件A中触发事件,发送数据 注意:接 ...
- Django项目之Web端电商网站的实战开发(三)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- 【Pycharm】【HTML】注释问题
学习HTML中,遇到的注释前存在空行的问题: 只要找到Pycharm设置中:勾选去掉即可
- SpringCloud组件和概念介绍
一:什么是微服务(Microservice) 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及 ...
- [AngularFire] Firebase OAuth Login With Custom Firestore User Data
import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...
- php 函数 pathinfo() 在分析文件名时,会含有上级目录
php 函数 pathinfo() 在分析文件名时,会含有上级目录对一些含有中文的多级目录,这个函数取得的文件名可能会包含上级目录,
- 7.Maven之(七)pom.xml配置文件详解
转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...