当前位置: 首页 > news >正文

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++ 并行编程-使用消息块筛选器

本文档演示了如何使用筛选器函数&#xff0c;使异步消息块能够根据消息的有效负载接受或拒绝消息。 创建消息块对象(例如 concurrency::unbounded_buffer、concurrency::call 或 concurrency::transformer)时&#xff0c;可以提供筛选器函数&#xff0c;用于确定消息块是接受还…...

【mysql技术内幕】

MySQL之技术内幕 1.MVCC模式2. 实现mvcc模式的基础点3.MySQL锁的类型4. 说下MySQL的索引有哪些吧&#xff1f;5. 谈谈分库分表6. 分表后的id咋么保证唯一性呢&#xff1f;7. 分表后非sharding key的查询咋么处理的&#xff1f; 1.MVCC模式 MVCC, 是multi-version concurrency c…...

快递物流单号识别API接口DEMO下载

单号识别API为用户提供单号识别快递公司服务&#xff0c;依托于快递鸟大数据平台&#xff0c;用户提供快递单号&#xff0c;即可实时返回可能的一个或多个快递公司&#xff0c;存在多个快递公司结果的&#xff0c;大数据平台根据可能性、单号量&#xff0c;进行智能排序。 应用…...

Jetpack——Room

概述 Room是谷歌公司推出的数据库处理框架&#xff0c;该框架同样基于SQLite&#xff0c;但它通过注解技术极大简化了数据库操作&#xff0c;减少了原来相当一部分编码工作量。在使用Room之前&#xff0c;要先修改模块的build.gradle文件&#xff0c;往dependencies节点添加下…...

Dynamic Connected Networks for Chinese Spelling Check(ACL2021)

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

前端vue-3种生命周期,只能在各自的领域使用

上面的表格可以简化为下面的两句话&#xff1a; setup是语法糖&#xff0c;下面的两个import导入是vue3和vue2的区别&#xff0c;现在的vue3直接导入&#xff0c;比之前vue2简单 还可以是导入两个生命周期函数...

el-upload如何自定展示上传的文件

Element UI 中&#xff0c;el-upload 组件支持通过插槽&#xff08;slot&#xff09;来自定义文件列表的展示方式。这通常是通过 file-list 插槽来实现的。下面是一个使用 el-upload 组件并通过 file-list 插槽来自定义文件列表展示的完整示例代码。 在这个示例中&#xff0c;…...

研1日记15

1. 文心一言生成&#xff1a; 在PyTorch中&#xff0c;nn.AdaptiveAvgPool1d(1)是一个一维自适应平均池化层。这个层的作用是将输入的特征图&#xff08;或称为张量&#xff09;在一维上进行自适应平均池化&#xff0c;使得输出特征图的大小在指定的维度上变为1。这意味着&…...

基于Nginx搭建点播直播服务器

实现直播和点播离不开服务器⽀持&#xff0c;可以使用开源的NGINX服务器搭建直播和点播服务。 当然&#xff0c;NGINX本身是不⽀持视频的&#xff0c;需要为NGINX增加相应的RTMP模块进行支持。 1、下载nginx和rtmp模块 # nginx wget ht tp://nginx.org/download/nginx-1.18.…...

QT LineEdit显示模式

QT LineEdit显示模式 QLineEdit 显示模式:   Normal 普通模式   NoEcho 不回写&#xff0c;即输入内容是有的&#xff0c;但是显示不出来,就是不在 QLineEdit 输入框中显示&#xff0c;但是触发例如 textChanged 信号会将所输入的文字写出来   Password 显示密码   Pa…...

IT技术在数字化转型中的关键作用

IT技术在数字化转型中的关键作用 在当今数字化浪潮中&#xff0c;IT技术无疑扮演着核心角色。无论是企业的数字化转型&#xff0c;还是政府公共服务的智能化提升&#xff0c;信息技术都在推动着整个社会向更高效、更智能的方向发展。本文将探讨IT技术在数字化转型中的关键作用…...

【C++指南】C++中nullptr的深入解析

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a;《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 核心库&#xff0c;它提供了一系列新的集合类型&#xff08;例如多映射 multimap 和多集合 multiset&#xff09;、不可变集合、图形库以及用于并发、I/O、哈希、原始类型、字符串等的实用工具。Guava 在 Google 的大多数 Java 项目中得到了广…...

Qt-qmake概述

概述 qmake工具为您提供了一个面向项目的系统&#xff0c;用于管理应用程序、库和其他组件的构建过程。这种方法使您能够控制使用的源文件&#xff0c;并允许简洁地描述过程中的每个步骤&#xff0c;通常在单个文件中。qmake将每个项目文件中的信息扩展为一个Makefile&#xf…...

【protobuf】ProtoBuf的学习与使用⸺C++

W...Y的主页 &#x1f60a; 代码仓库分享&#x1f495; 前言&#xff1a;之前我们学习了Linux与windows的protobuf安装&#xff0c;知道protobuf是做序列化操作的应用&#xff0c;今天我们来学习一下protobuf。 目录 ⼀、初识ProtoBuf 步骤1&#xff1a;创建.proto文件 步…...

【iOS】MVC架构模式

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

ML 系列:机器学习和深度学习的深层次总结(08)—欠拟合、过拟合,正确拟合

ML 系列赛&#xff1a;第 9 天 — Under、Over 和 Good Fit 文章目录 一、说明二、了解欠拟合、过拟合和实现正确的平衡三、关于泛化四、欠拟合五、过拟合六、适度拟合七、结论 一、说明 在有监督学习过程中&#xff0c;对于指定数据集进行训练&#xff0c;训练结果存在欠拟合…...

Unity-物理系统-刚体加力

一 刚体自带添加力的方法 给刚体加力的目标就是 让其有一个速度 朝向某一个方向移动 1.首先应该获取刚体组件 rigidBody this.GetComponent<Rigidbody>(); 2.添加力 //相对世界坐标 //世界坐标系 Z轴正方向加了一个里 //加力过后 对象是否停止…...

深入探究PR:那些被忽视却超实用的视频剪辑工具

如果想要了解视频剪辑的工具&#xff0c;那一定听说过pr视频剪辑吧。如果你是新手其实我更推荐你从简单的视频剪辑工具入手&#xff0c;这次我就介绍一些简单好操作的视频剪辑工具来入门吧。 1.福晰视频剪辑 连接直达>>https://www.pdf365.cn/foxit-clip/ 这款工具操…...

Unity-麦克风输入相关

private AudioClip clip; 知识点一 获取设备麦克风信息 string[] strs Microphone.devices; for (int i 0; i < strs.Length; i) { print(strs[i]); } 知识点二 开始录制 参数一&#xff1a;设备名 传空使用默认设备 参数二&#xff1a;超过录…...

NLP--自然语言处理学习-day1

一.初步认识NLP 自然语言处理&#xff08;Natural Language Processing, NLP&#xff09;是计算机科学和人工智能&#xff08;AI&#xff09;的一个交叉领域&#xff0c;旨在使计算机能够理解、分析、生成和处理人类语言的能力。它结合了计算语言学、人工智能、机器学习和语言…...

ER论文阅读-Incomplete Multimodality-Diffused Emotion Recognition

基本介绍&#xff1a;NeurIPS, 2024, CCF-A 原文链接&#xff1a;https://proceedings.neurips.cc/paper_files/paper/2023/file/372cb7805eaccb2b7eed641271a30eec-Paper-Conference.pdf Abstract 人类多模态情感识别&#xff08;MER&#xff09;旨在通过多种异质模态&#x…...

Matlab自学笔记36:日期时间型的概念、分类和创建方法

1.概念 日期时间型&#xff08;Dates and Time&#xff09;数据具有灵活的显示格式和高达毫微秒的精度&#xff0c;并且可以处理时区、夏令时和平闰年等特殊因素 2.日期时间型数据有以下三种表示方式 &#xff08;1&#xff09;Datetime型&#xff0c;表示日期时间点&#x…...

Spring Boot自定义配置项

Spring Boot自定义配置项 配置文件 在application.properties文件添加需要的配置 比如&#xff1a; file.pathD:\\flies\\springboot\\ConfigurationProperties 注解 使用注解ConfigurationProperties将配置项和实体Bean关联起来&#xff0c;实现配置项和实体类字段的关联&…...

【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略

文章目录 C类与对象前言读者须知RVO 与 NRVO 的启用条件如何确认优化是否启用&#xff1f; 1. 按值传递与拷贝省略1.1 按值传递的概念1.2 示例代码1.3 按值传递的性能影响1.3.1 完全不优化 1.4 不同编译器下的优化表现1.4.1 Visual Studio 2019普通优化1.4.2 Visual Studio 202…...

什么时候用synchronized,什么时候用Reentrantlock

文章目录 使用 synchronized 的场景使用 ReentrantLock 的场景综合考虑 使用 synchronized 的场景 synchronized 是 Java 内置的同步机制&#xff0c;使用起来比较简单且常用于如下场景&#xff1a; 1、简单的同步逻辑&#xff1a;当你的同步逻辑非常简单&#xff0c;比如只需…...

[ffmpeg]音频格式转换

本文主要梳理 ffmpeg 中的音频格式转换。由于采集的音频数据和编码器支持的音频格式可能不一样&#xff0c;所以经常需要进行格式转换。 API 调用 常用 API struct SwrContext *swr_alloc(void); int swr_init(struct SwrContext *s); struct SwrContext *swr_alloc_set_opt…...

SSRF工具类-SsrfTool

为了帮助开发人员和安全研究人员检测和修复SSRF(Server-Side Request Forgery)漏洞,存在 多种工具。这里我将给出一个简单的工具类示例,这个工具类可以用来检查一个给定的URL是否可 能引发SSRF攻击。请注意,这个工具类主要用于教育目的,并不意味着它可以完全防止所有的…...

python集合运算介绍及示例代码

Python 中的集合&#xff08;set&#xff09;是一种数据类型&#xff0c;用于存储唯一元素的无序集合。集合支持多种运算&#xff0c;如并集、交集、差集和对称差集&#xff0c;方便执行数学上的集合操作。 1. 创建集合 可以使用大括号 {} 或者 set() 函数创建集合&#xff1…...