windows C++ 并行编程-使用消息块筛选器
本文档演示了如何使用筛选器函数,使异步消息块能够根据消息的有效负载接受或拒绝消息。
创建消息块对象(例如 concurrency::unbounded_buffer、concurrency::call 或 concurrency::transformer)时,可以提供筛选器函数,用于确定消息块是接受还是拒绝消息。 筛选器函数是保证消息块仅接收特定值的有效方式。
筛选器函数很重要,因为它们使你能够连接消息块以形成数据流网络。 在数据流网络中,消息块通过仅处理满足特定标准的消息来控制数据流。 将数据流网络与控制流模型进行比较,在后者中,数据流是通过使用条件语句、循环等控制结构来调节的。
本文档提供了有关如何使用消息筛选器的基本示例。
示例:count_primes 函数
考虑以下函数 count_primes,该函数说明了不筛选传入消息的消息块的基本用法。 此消息块将质数追加到 std::vector 对象。 count_primes 函数将几个数字发送到消息块,从消息块接收输出值,并将这些数字打印到控制台。
// Illustrates usage of a message buffer that does not use filtering.
void count_primes(unsigned long random_seed)
{// Holds prime numbers.vector<unsigned long> primes;// Adds numbers that are prime to the vector object.transformer<unsigned long, unsigned long> t([&primes](unsigned long n) -> unsigned long{if (is_prime(n)){primes.push_back(n);}return n;});// Send random values to the message buffer.mt19937 generator(random_seed);for (int i = 0; i < 20; ++i){send(t, static_cast<unsigned long>(generator()%10000));}// Receive from the message buffer the same number of times// to ensure that the message buffer has processed each message.for (int i = 0; i < 20; ++i){receive(t);}// Print the prime numbers to the console.wcout << L"The following numbers are prime: " << endl;for(unsigned long prime : primes){wcout << prime << endl;}
}
transformer 对象处理所有输入值;但是,它只需要那些为质数的值。 尽管可以编写应用程序以使消息发送方仅发送质数,但并不能始终得知消息接收方的要求。
示例:count_primes_filter 函数
以下函数 count_primes_filter 执行与 count_primes 函数相同的任务。 但是,此版本中的 transformer 对象使用筛选器函数以便仅接受那些为质数的值。 执行该操作的函数仅接收质数;因此,它不必调用 is_prime 函数。
由于 transformer 对象仅接收质数,所以 transformer 对象本身可以保存质数。 换言之,此示例中的 transformer 对象不需要将质数添加到 vector 对象。
// Illustrates usage of a message buffer that uses filtering.
void count_primes_filter(unsigned long random_seed)
{// Accepts numbers that are prime.transformer<unsigned long, unsigned long> t([](unsigned long n) -> unsigned long{// The filter function guarantees that the input value is prime.// Return the input value.return n;},nullptr,[](unsigned long n) -> bool{// Filter only values that are prime.return is_prime(n);});// Send random values to the message buffer.mt19937 generator(random_seed);size_t prime_count = 0;for (int i = 0; i < 20; ++i){if (send(t, static_cast<unsigned long>(generator()%10000))){++prime_count;}}// Print the prime numbers to the console. wcout << L"The following numbers are prime: " << endl;while (prime_count-- > 0){wcout << receive(t) << endl;}
}
transformer 对象现在仅处理那些为质数的值。 在前面的示例中,transformer 对象处理所有消息。 因此,前面的示例必须接收与其发送的相同数量的消息。 此示例使用 concurrency::send 函数的结果来确定要从 transformer 对象接收的消息数。 send 函数在消息缓冲区接受消息时返回 true,在消息缓冲区拒绝消息时返回 false。 因此,消息缓冲区接受消息的次数与质数的计数相匹配。
示例:已完成的消息块筛选器代码示例
以下代码显示完整示例。 该示例同时调用 count_primes 函数和 count_primes_filter 函数。
// primes-filter.cpp
// compile with: /EHsc
#include <agents.h>
#include <algorithm>
#include <iostream>
#include <random>using namespace concurrency;
using namespace std;// Determines whether the input value is prime.
bool is_prime(unsigned long n)
{if (n < 2)return false;for (unsigned long i = 2; i < n; ++i){if ((n % i) == 0)return false;}return true;
}// Illustrates usage of a message buffer that does not use filtering.
void count_primes(unsigned long random_seed)
{// Holds prime numbers.vector<unsigned long> primes;// Adds numbers that are prime to the vector object.transformer<unsigned long, unsigned long> t([&primes](unsigned long n) -> unsigned long{if (is_prime(n)){primes.push_back(n);}return n;});// Send random values to the message buffer.mt19937 generator(random_seed);for (int i = 0; i < 20; ++i){send(t, static_cast<unsigned long>(generator()%10000));}// Receive from the message buffer the same number of times// to ensure that the message buffer has processed each message.for (int i = 0; i < 20; ++i){receive(t);}// Print the prime numbers to the console.wcout << L"The following numbers are prime: " << endl;for(unsigned long prime : primes){wcout << prime << endl;}
}// Illustrates usage of a message buffer that uses filtering.
void count_primes_filter(unsigned long random_seed)
{// Accepts numbers that are prime.transformer<unsigned long, unsigned long> t([](unsigned long n) -> unsigned long{// The filter function guarantees that the input value is prime.// Return the input value.return n;},nullptr,[](unsigned long n) -> bool{// Filter only values that are prime.return is_prime(n);});// Send random values to the message buffer.mt19937 generator(random_seed);size_t prime_count = 0;for (int i = 0; i < 20; ++i){if (send(t, static_cast<unsigned long>(generator()%10000))){++prime_count;}}// Print the prime numbers to the console. wcout << L"The following numbers are prime: " << endl;while (prime_count-- > 0){wcout << receive(t) << endl;}
}int wmain()
{const unsigned long random_seed = 99714;wcout << L"Without filtering:" << endl;count_primes(random_seed);wcout << L"With filtering:" << endl;count_primes_filter(random_seed);/* Output:99739349924188931297712786473229With filtering:The following numbers are prime:99739349924188931297712786473229*/
}
可靠编程
筛选器函数可以是 lambda 函数、函数指针或函数对象。 每个筛选器函数采用以下格式之一:
bool (T)
bool (T const &)
为了消除不必要的数据复制,需要按值传输聚合类型时,请使用第二种格式。
相关文章:
windows C++ 并行编程-使用消息块筛选器
本文档演示了如何使用筛选器函数,使异步消息块能够根据消息的有效负载接受或拒绝消息。 创建消息块对象(例如 concurrency::unbounded_buffer、concurrency::call 或 concurrency::transformer)时,可以提供筛选器函数,用于确定消息块是接受还…...

【mysql技术内幕】
MySQL之技术内幕 1.MVCC模式2. 实现mvcc模式的基础点3.MySQL锁的类型4. 说下MySQL的索引有哪些吧?5. 谈谈分库分表6. 分表后的id咋么保证唯一性呢?7. 分表后非sharding key的查询咋么处理的? 1.MVCC模式 MVCC, 是multi-version concurrency c…...
快递物流单号识别API接口DEMO下载
单号识别API为用户提供单号识别快递公司服务,依托于快递鸟大数据平台,用户提供快递单号,即可实时返回可能的一个或多个快递公司,存在多个快递公司结果的,大数据平台根据可能性、单号量,进行智能排序。 应用…...
Jetpack——Room
概述 Room是谷歌公司推出的数据库处理框架,该框架同样基于SQLite,但它通过注解技术极大简化了数据库操作,减少了原来相当一部分编码工作量。在使用Room之前,要先修改模块的build.gradle文件,往dependencies节点添加下…...

Dynamic Connected Networks for Chinese Spelling Check(ACL2021)
Dynamic Connected Networks for Chinese Spelling Check(ACL2021) 一.概述 文中认为基于bert的非自回归语言模型依赖于输出独立性假设。不适当的独立性假设阻碍了基于bert的模型学习目标token之间的依赖关系,从而导致了不连贯的问题。为些,…...

前端vue-3种生命周期,只能在各自的领域使用
上面的表格可以简化为下面的两句话: setup是语法糖,下面的两个import导入是vue3和vue2的区别,现在的vue3直接导入,比之前vue2简单 还可以是导入两个生命周期函数...
el-upload如何自定展示上传的文件
Element UI 中,el-upload 组件支持通过插槽(slot)来自定义文件列表的展示方式。这通常是通过 file-list 插槽来实现的。下面是一个使用 el-upload 组件并通过 file-list 插槽来自定义文件列表展示的完整示例代码。 在这个示例中,…...
研1日记15
1. 文心一言生成: 在PyTorch中,nn.AdaptiveAvgPool1d(1)是一个一维自适应平均池化层。这个层的作用是将输入的特征图(或称为张量)在一维上进行自适应平均池化,使得输出特征图的大小在指定的维度上变为1。这意味着&…...

基于Nginx搭建点播直播服务器
实现直播和点播离不开服务器⽀持,可以使用开源的NGINX服务器搭建直播和点播服务。 当然,NGINX本身是不⽀持视频的,需要为NGINX增加相应的RTMP模块进行支持。 1、下载nginx和rtmp模块 # nginx wget ht tp://nginx.org/download/nginx-1.18.…...
QT LineEdit显示模式
QT LineEdit显示模式 QLineEdit 显示模式: Normal 普通模式 NoEcho 不回写,即输入内容是有的,但是显示不出来,就是不在 QLineEdit 输入框中显示,但是触发例如 textChanged 信号会将所输入的文字写出来 Password 显示密码 Pa…...
IT技术在数字化转型中的关键作用
IT技术在数字化转型中的关键作用 在当今数字化浪潮中,IT技术无疑扮演着核心角色。无论是企业的数字化转型,还是政府公共服务的智能化提升,信息技术都在推动着整个社会向更高效、更智能的方向发展。本文将探讨IT技术在数字化转型中的关键作用…...

【C++指南】C++中nullptr的深入解析
💓 博客主页:倔强的石头的CSDN主页 📝Gitee主页:倔强的石头的gitee主页 ⏩ 文章专栏:《C指南》 期待您的关注 目录 引言 一、nullptr的引入背景 二、nullptr的特点 1.类型安全 2.明确的空指针表示 3.函数重载支…...

解决启动docker desktop报The network name cannot be found的问题
现象 deploying WSL2 distributions ensuring main distro is deployed: checking if main distro is up to date: checking main distro bootstrap version: getting main distro bootstrap version: open \wsl$\docker-desktop\etc\wsl_bootstrap_version: The network name…...
Guava: 探索 Google 的 Java 核心库
Guava 是 Google 开发的一套 Java 核心库,它提供了一系列新的集合类型(例如多映射 multimap 和多集合 multiset)、不可变集合、图形库以及用于并发、I/O、哈希、原始类型、字符串等的实用工具。Guava 在 Google 的大多数 Java 项目中得到了广…...
Qt-qmake概述
概述 qmake工具为您提供了一个面向项目的系统,用于管理应用程序、库和其他组件的构建过程。这种方法使您能够控制使用的源文件,并允许简洁地描述过程中的每个步骤,通常在单个文件中。qmake将每个项目文件中的信息扩展为一个Makefile…...

【protobuf】ProtoBuf的学习与使用⸺C++
W...Y的主页 😊 代码仓库分享💕 前言:之前我们学习了Linux与windows的protobuf安装,知道protobuf是做序列化操作的应用,今天我们来学习一下protobuf。 目录 ⼀、初识ProtoBuf 步骤1:创建.proto文件 步…...

【iOS】MVC架构模式
文章目录 前言MVC架构模式基本概念通信方式简单应用 总结 前言 “MVC”,即Model(模型),View(视图),Controller(控制器),MVC模式是架构模式的一种。 关于“架构模式”&a…...

ML 系列:机器学习和深度学习的深层次总结(08)—欠拟合、过拟合,正确拟合
ML 系列赛:第 9 天 — Under、Over 和 Good Fit 文章目录 一、说明二、了解欠拟合、过拟合和实现正确的平衡三、关于泛化四、欠拟合五、过拟合六、适度拟合七、结论 一、说明 在有监督学习过程中,对于指定数据集进行训练,训练结果存在欠拟合…...
Unity-物理系统-刚体加力
一 刚体自带添加力的方法 给刚体加力的目标就是 让其有一个速度 朝向某一个方向移动 1.首先应该获取刚体组件 rigidBody this.GetComponent<Rigidbody>(); 2.添加力 //相对世界坐标 //世界坐标系 Z轴正方向加了一个里 //加力过后 对象是否停止…...

深入探究PR:那些被忽视却超实用的视频剪辑工具
如果想要了解视频剪辑的工具,那一定听说过pr视频剪辑吧。如果你是新手其实我更推荐你从简单的视频剪辑工具入手,这次我就介绍一些简单好操作的视频剪辑工具来入门吧。 1.福晰视频剪辑 连接直达>>https://www.pdf365.cn/foxit-clip/ 这款工具操…...
挑战杯推荐项目
“人工智能”创意赛 - 智能艺术创作助手:借助大模型技术,开发能根据用户输入的主题、风格等要求,生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用,帮助艺术家和创意爱好者激发创意、提高创作效率。 - 个性化梦境…...
线程与协程
1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指:像函数调用/返回一样轻量地完成任务切换。 举例说明: 当你在程序中写一个函数调用: funcA() 然后 funcA 执行完后返回&…...

免费数学几何作图web平台
光锐软件免费数学工具,maths,数学制图,数学作图,几何作图,几何,AR开发,AR教育,增强现实,软件公司,XR,MR,VR,虚拟仿真,虚拟现实,混合现实,教育科技产品,职业模拟培训,高保真VR场景,结构互动课件,元宇宙http://xaglare.c…...
HTML前端开发:JavaScript 获取元素方法详解
作为前端开发者,高效获取 DOM 元素是必备技能。以下是 JS 中核心的获取元素方法,分为两大系列: 一、getElementBy... 系列 传统方法,直接通过 DOM 接口访问,返回动态集合(元素变化会实时更新)。…...

【堆垛策略】设计方法
堆垛策略的设计是积木堆叠系统的核心,直接影响堆叠的稳定性、效率和容错能力。以下是分层次的堆垛策略设计方法,涵盖基础规则、优化算法和容错机制: 1. 基础堆垛规则 (1) 物理稳定性优先 重心原则: 大尺寸/重量积木在下…...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]
报错信息:libc.so.6: cannot open shared object file: No such file or directory: #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...

6.9-QT模拟计算器
源码: 头文件: widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QMouseEvent>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent nullptr);…...

网页端 js 读取发票里的二维码信息(图片和PDF格式)
起因 为了实现在报销流程中,发票不能重用的限制,发票上传后,希望能读出发票号,并记录发票号已用,下次不再可用于报销。 基于上面的需求,研究了OCR 的方式和读PDF的方式,实际是可行的ÿ…...
GeoServer发布PostgreSQL图层后WFS查询无主键字段
在使用 GeoServer(版本 2.22.2) 发布 PostgreSQL(PostGIS)中的表为地图服务时,常常会遇到一个小问题: WFS 查询中,主键字段(如 id)莫名其妙地消失了! 即使你在…...

RushDB开源程序 是现代应用程序和 AI 的即时数据库。建立在 Neo4j 之上
一、软件介绍 文末提供程序和源码下载 RushDB 改变了您处理图形数据的方式 — 不需要 Schema,不需要复杂的查询,只需推送数据即可。 二、Key Features ✨ 主要特点 Instant Setup: Be productive in seconds, not days 即时设置 :在几秒钟…...