windows C++-创建数据流代理(二)
完整的数据流演示
下图显示了 dataflow_agent 类的完整数据流网络:
由于 run 方法是在一个单独的线程上调用的,因此在完全连接网络之前,其他线程可以将消息发送到网络。 _source 数据成员是一个 unbounded_buffer 对象,用于缓冲从应用程序发送到代理的所有输入。 为了确保网络能够处理所有输入消息,代理会首先链接网络的内部节点,然后将该网络的起点 connector 链接到 _source 数据成员。 这可以保证在形成网络的过程中不会处理消息。
由于此示例中的网络是基于数据流,而不是基于控制流的,网络必须向代理传达它已经完成了对每个输入值的处理,并且 Sentinel 节点也已接收到它的值。 此示例使用 countdown_event 对象来指示所有输入值均已经过处理,使用 concurrency::event 对象指示 Sentinel 节点已接收到它的值。 countdown_event 类使用 event 对象指示计数器值达到零。 每当数据流网络的头收到一个值时,都会递增计数器值。 在处理输入值后,网络的每个终端节点都会递减计数器值。 代理形成数据流网络后,它会等待 Sentinel 节点设置 event 对象,还会等待 countdown_event 对象指示其计数器值已达到零。
下面的示例展示了 control_flow_agent、dataflow_agent 和 countdown_event 类。 wmain 函数创建了 control_flow_agent 和 dataflow_agent 对象,并使用 send_values 函数将一系列随机值发送到代理。
// dataflow-agent.cpp
// compile with: /EHsc
#include <windows.h>
#include <agents.h>
#include <iostream>
#include <random>using namespace concurrency;
using namespace std;// A basic agent that uses control-flow to regulate the order of program
// execution. This agent reads numbers from a message buffer and counts the
// number of positive and negative values.
class control_flow_agent : public agent
{
public:explicit control_flow_agent(ISource<int>& source): _source(source){}// Retrieves the count of negative numbers that the agent received.size_t negatives() {return receive(_negatives);}// Retrieves the count of positive numbers that the agent received.size_t positives(){return receive(_positives);}protected:void run(){// Counts the number of negative and positive values that// the agent receives.size_t negative_count = 0;size_t positive_count = 0;// Read from the source buffer until we receive// the sentinel value of 0.int value = 0; while ((value = receive(_source)) != 0){// Send negative values to the first target and// non-negative values to the second target.if (value < 0)++negative_count;else++positive_count;}// Write the counts to the message buffers.send(_negatives, negative_count);send(_positives, positive_count);// Set the agent to the completed state.done();}
private:// Source message buffer to read from.ISource<int>& _source;// Holds the number of negative and positive numbers that the agent receives.single_assignment<size_t> _negatives;single_assignment<size_t> _positives;
};// A synchronization primitive that is signaled when its
// count reaches zero.
class countdown_event
{
public:countdown_event(unsigned int count = 0L): _current(static_cast<long>(count)) {// Set the event if the initial count is zero.if (_current == 0L)_event.set();}// Decrements the event counter.void signal() {if(InterlockedDecrement(&_current) == 0L) {_event.set();}}// Increments the event counter.void add_count() {if(InterlockedIncrement(&_current) == 1L) {_event.reset();}}// Blocks the current context until the event is set.void wait() {_event.wait();}private:// The current count.volatile long _current;// The event that is set when the counter reaches zero.event _event;// Disable copy constructor.countdown_event(const countdown_event&);// Disable assignment.countdown_event const & operator=(countdown_event const&);
};// A basic agent that resembles control_flow_agent, but uses uses dataflow to
// perform computations when data becomes available.
class dataflow_agent : public agent
{
public:dataflow_agent(ISource<int>& source): _source(source){}// Retrieves the count of negative numbers that the agent received.size_t negatives() {return receive(_negatives);}// Retrieves the count of positive numbers that the agent received.size_t positives(){return receive(_positives);}protected:void run(){// Counts the number of negative and positive values that// the agent receives.size_t negative_count = 0;size_t positive_count = 0;// Tracks the count of active operations.countdown_event active;// An event that is set by the sentinel.event received_sentinel;//// Create the members of the dataflow network.//// Increments the active counter.transformer<int, int> increment_active([&active](int value) -> int {active.add_count();return value;});// Increments the count of negative values.call<int> negatives([&](int value) {++negative_count;// Decrement the active counter.active.signal();},[](int value) -> bool {return value < 0;});// Increments the count of positive values.call<int> positives([&](int value) {++positive_count;// Decrement the active counter.active.signal();},[](int value) -> bool {return value > 0;});// Receives only the sentinel value of 0.call<int> sentinel([&](int value) { // Decrement the active counter.active.signal();// Set the sentinel event.received_sentinel.set();},[](int value) -> bool { return value == 0; });// Connects the _source message buffer to the rest of the network.unbounded_buffer<int> connector;//// Connect the network.//// Connect the internal nodes of the network.connector.link_target(&negatives);connector.link_target(&positives);connector.link_target(&sentinel);increment_active.link_target(&connector);// Connect the _source buffer to the internal network to // begin data flow._source.link_target(&increment_active);// Wait for the sentinel event and for all operations to finish.received_sentinel.wait();active.wait();// Write the counts to the message buffers.send(_negatives, negative_count);send(_positives, positive_count);// Set the agent to the completed state.done();}private:// Source message buffer to read from.ISource<int>& _source;// Holds the number of negative and positive numbers that the agent receives.single_assignment<size_t> _negatives;single_assignment<size_t> _positives;
};// Sends a number of random values to the provided message buffer.
void send_values(ITarget<int>& source, int sentinel, size_t count)
{// Send a series of random numbers to the source buffer.mt19937 rnd(42);for (size_t i = 0; i < count; ++i){// Generate a random number that is not equal to the sentinel value.int n;while ((n = rnd()) == sentinel);send(source, n); }// Send the sentinel value.send(source, sentinel);
}int wmain()
{// Signals to the agent that there are no more values to process.const int sentinel = 0;// The number of samples to send to each agent.const size_t count = 1000000;// The source buffer that the application writes numbers to and // the agents read numbers from.unbounded_buffer<int> source;//// Use a control-flow agent to process a series of random numbers.//wcout << L"Control-flow agent:" << endl;// Create and start the agent.control_flow_agent cf_agent(source);cf_agent.start();// Send values to the agent.send_values(source, sentinel, count);// Wait for the agent to finish.agent::wait(&cf_agent);// Print the count of negative and positive numbers.wcout << L"There are " << cf_agent.negatives() << L" negative numbers."<< endl;wcout << L"There are " << cf_agent.positives() << L" positive numbers."<< endl; //// Perform the same task, but this time with a dataflow agent.//wcout << L"Dataflow agent:" << endl;// Create and start the agent.dataflow_agent df_agent(source);df_agent.start();// Send values to the agent.send_values(source, sentinel, count);// Wait for the agent to finish.agent::wait(&df_agent);// Print the count of negative and positive numbers.wcout << L"There are " << df_agent.negatives() << L" negative numbers."<< endl;wcout << L"There are " << df_agent.positives() << L" positive numbers."<< endl;
}
输出如下:
Control-flow agent:
There are 500523 negative numbers.
There are 499477 positive numbers.
Dataflow agent:
There are 500523 negative numbers.
There are 499477 positive numbers.
编译代码
复制示例代码,并将它粘贴到 Visual Studio 项目中,或粘贴到名为 dataflow-agent.cpp
的文件中,再在 Visual Studio 命令提示符窗口中运行以下命令。
cl.exe /EHsc dataflow-agent.cpp
相关文章:

windows C++-创建数据流代理(二)
完整的数据流演示 下图显示了 dataflow_agent 类的完整数据流网络: 由于 run 方法是在一个单独的线程上调用的,因此在完全连接网络之前,其他线程可以将消息发送到网络。 _source 数据成员是一个 unbounded_buffer 对象,用于缓冲…...

大数据毕业设计选题推荐-个性化图书推荐系统-Python数据可视化-Hive-Hadoop-Spark
✨作者主页:IT毕设梦工厂✨ 个人简介:曾从事计算机专业培训教学,擅长Java、Python、PHP、.NET、Node.js、GO、微信小程序、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇…...

【Redis入门到精通九】Redis中的主从复制
目录 主从复制 1.配置主从复制 2.主从复制中的拓扑结构 3.主从复制原理 4.主从复制总结 主从复制 在分布式系统中为了解决单点问题,通常会把数据复制多个副本部署到其他服务器,满⾜故障恢复和负载均衡等需求。Redis 也是如此,它为我们提…...

系统架构设计师论文《论企业应用系统的数据持久层架构设计》精选试读
论文真题 数据持久层(Data Persistence Layer)通常位于企业应用系统的业务逻辑层和数据源层之间,为整个项目提供一个高层、统一、安全、并发的数据持久机制,完成对各种数据进行持久化的编程工作,并为系统业务逻辑层提…...
策略模式和模板模式的区别
目录 一、实现方式 策略模式 模板模式 二、使用场景 三、优点 四、举例 一、实现方式 策略模式 定义策略接口 Strategy创建具体策略类 OperationAdd、OperationSubtract、OperationMultiply创建一个上下文类 Context,包含一个策略对象的引用,并通…...

【ubuntu】ubuntu20.04安装conda
1.下载 安装参考:https://blog.csdn.net/weixin_44119391/article/details/128577681 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 2.安装 sudo chmod 777 -R ./Anaconda3-5.3.1-Linux-x86_64.sh ./Anaconda3-5.3.1-Linux-x86_64.sh Enter键确认安装…...

使用 SAP ABAP Webdynpro 实现 ABAP Push Channel 的 Web Socket 客户端
本系列前三篇文章,笔者向大家介绍了基于 ABAP Push Channel(简称 APC)的 TCP Socket 服务器端和客户端的编程,以及 Web Socket 的服务器端实现。 使用 ABAP 实现 TCP Socket 编程 (1) - 客户端部分的实现使用 ABAP 实现 TCP Socket 编程 (2) - 服务器端部分的实现使用 ABAP 实…...

15分钟学 Python 第41天:Python 爬虫入门(六)第二篇
Day41:Python爬取猫眼电影网站的电影信息 1. 项目背景 在本项目中,我们将使用 Python 爬虫技术从猫眼电影网站抓取电影信息。猫眼电影是一个知名的电影信息平台,提供了丰富的电影相关数据。通过这个练习,您将深入学习如何抓取动…...

电脑提示d3dcompiler_47.dll缺失怎么修复,仔细介绍dll的解决方法
1. d3dcompiler_47.dll 概述 1.1 定义与作用 d3dcompiler_47.dll 是 Microsoft DirectX 的一个关键组件,作为一个动态链接库(DLL)文件,它在 Windows 操作系统中扮演着至关重要的角色。DirectX 是一套由微软开发的用于处理多媒体…...

CPU中的寄存器是什么以及它的工作原理是什么?
在计算机科学中,寄存器是数字设备中的一个重要组成部分,它用于存储数据和指令以快速处理。寄存器充当临时存储区,信息可以在这里被快速访问和操作,以执行复杂任务。寄存器是计算机中最基础的存储类型,它们在帮助机器高…...

【EXCEL数据处理】000021 案例 保姆级教程,附多个操作案例。EXCEL文档安全性设置。
前言:哈喽,大家好,今天给大家分享一篇文章!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏关注哦 💕 目录 【EXCEL数据处理】000021 案例 保姆级教程,附多个操作案例。…...
windows7 32bit安装JDK以及EclipseEE
如果你的电脑是 Windows 7 32-bit 系统,那么需要下载并安装适用于 32-bit 系统的 JDK 和 Eclipse EE。以下是具体的步骤和下载链接: 1. 下载并安装适用于 Windows 32-bit 的 JDK 1.1 下载适用于 32-bit 的 JDK Oracle 不再提供最新版本的 32-bit JDK&…...
Python中的Enum
Python中的Enum Enum(枚举)在很多应用场景中都会出现,因此绝大部分编程语言都实现了Enum类型,Python也不列外,但列外的是Enum在Python3.4中才被正式支持,我们先来看看Python3中的Enum是怎么使用的。 枚举的…...
于BERT的中文问答系统12
主要改进点 日志配置: 确保日志文件按日期和时间生成,便于追踪不同运行的记录。 数据处理: 增加了对数据加载过程中错误的捕获和日志记录,确保程序能够跳过无效数据并继续运行。 模型训练: 增加了重新训练模型的功…...

基于SpringBoot“花开富贵”花园管理系统【附源码】
效果如下: 系统注册页面 系统首页界面 植物信息详细页面 后台登录界面 管理员主界面 植物分类管理界面 植物信息管理界面 园艺记录管理界面 研究背景 随着城市化进程的加快和人们生活质量的提升,越来越多的人开始追求与自然和谐共生的生活方式…...

MySQL连接查询:自连接
先看我的表结构 emp表 自连接也就是把一个表看作是两个作用的表就好,也就是说我把emp看作员工表,也看做领导表 自连接 基本语法 select 字段列表 FROM 表A 别名A JOIN 表A 别名B ON 条件;例子1:查询员工 及其 所属领导的名字 select a.n…...
Prometheus+Grafana备忘
Grafana安装 官网 https://grafana.com/grafana/download 官网提供了几种安装方式,我用最简单的 yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.2.2-1.x86_64.rpm启动 //如果需要在系统启动时自动启动Grafana,可以…...

基于ssm实现的建筑装修图纸管理平台(源码+文档)
项目简介 基于ssm实现的建筑装修图纸管理平台,主要功能如下: 技术栈 后端框框:spring/springmvc/mybatis 前端框架:html/JavaScript/Css/vue/elementui 运行环境:JDK1.8/MySQL5.7/idea(可选)…...

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07 目录 文章目录 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07目录1. Evaluation of Large Language Models for Summarization Tasks in the Medical Domain: A Narrative Review摘要研究…...
Mahalanobis distance 马哈拉诺比斯距离
马哈拉诺比斯距离(Mahalanobis Distance)是一种衡量点与分布之间距离的度量,尤其适用于多维数据。与欧几里得距离不同,马哈拉诺比斯距离考虑了数据的协方差结构,因此在统计分析和异常值检测中非常有用。 定义 给定一…...

大数据学习栈记——Neo4j的安装与使用
本文介绍图数据库Neofj的安装与使用,操作系统:Ubuntu24.04,Neofj版本:2025.04.0。 Apt安装 Neofj可以进行官网安装:Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...

MongoDB学习和应用(高效的非关系型数据库)
一丶 MongoDB简介 对于社交类软件的功能,我们需要对它的功能特点进行分析: 数据量会随着用户数增大而增大读多写少价值较低非好友看不到其动态信息地理位置的查询… 针对以上特点进行分析各大存储工具: mysql:关系型数据库&am…...

CMake基础:构建流程详解
目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...
postgresql|数据库|只读用户的创建和删除(备忘)
CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

C# 类和继承(抽象类)
抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)
UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中,UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化…...

Redis数据倾斜问题解决
Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中,部分节点存储的数据量或访问量远高于其他节点,导致这些节点负载过高,影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

Mysql中select查询语句的执行过程
目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析(Parser) 2.4、执行sql 1. 预处理(Preprocessor) 2. 查询优化器(Optimizer) 3. 执行器…...
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...