windows C++-同步数据结构与 Windows API

 将并发运行时提供的同步数据结构的行为与 Windows API 提供的同步数据结构的行为进行比较。

并发运行时提供的同步数据结构遵循协作线程模型。 在协作线程模型中,同步基元显式将其处理资源传递给其他线程。 这与抢占式线程模型不同,其中处理资源由控制调度程序或操作系统转移到其他线程。

critical_section

concurrency::critical_section 类类似于 Windows CRITICAL_SECTION 结构,因为它只能由一个进程的线程使用。 

reader_writer_lock

concurrency::reader_writer_lock 类类似于 Windows 精简读取器/编写器 (SRW) 锁。

event

concurrency::event 类类似于未命名的 Windows 手动重置事件。 不过,event 对象以协作方式运行,而 Windows 事件以抢占方式运行。

示例说明

若要更好地了解 event 类和 Windows 事件之间的区别,请考虑以下示例。 此示例使调度程序最多可以同时创建两个任务,然后调用两个使用 event 类和 Windows 手动重置事件的类似函数。 每个函数首先创建几个等待共享事件发出信号的任务。 然后,每个函数都会转移到正在运行的任务并向事件发出信号。 之后,每个函数等待信号事件。

// event-comparison.cpp
// compile with: /EHsc
#include <windows.h>
#include <concrtrm.h>
#include <ppl.h>
#include <iostream>
#include <sstream>using namespace concurrency;
using namespace std;// Demonstrates the usage of cooperative events.
void RunCooperativeEvents()
{// An event object.event e;// Create a task group and execute five tasks that wait for// the event to be set.task_group tasks;for (int i = 0; i < 5; ++i){tasks.run([&] {// Print a message before waiting on the event.wstringstream ss;ss << L"\t\tContext " << GetExecutionContextId() << L": waiting on an event." << endl; wcout << ss.str();// Wait for the event to be set.e.wait();// Print a message after the event is set.ss = wstringstream();ss << L"\t\tContext " << GetExecutionContextId() << L": received the event." << endl; wcout << ss.str();});}// Wait a sufficient amount of time for all tasks to enter // the waiting state.Sleep(1000L);// Set the event.wstringstream ss;ss << L"\tSetting the event." << endl; wcout << ss.str();e.set();// Wait for all tasks to complete.tasks.wait();
}// Demonstrates the usage of preemptive events.
void RunWindowsEvents()
{// A Windows event object.HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("Windows Event"));// Create a task group and execute five tasks that wait for// the event to be set.task_group tasks;for (int i = 0; i < 5; ++i){tasks.run([&] {// Print a message before waiting on the event.wstringstream ss;ss << L"\t\tContext " << GetExecutionContextId() << L": waiting on an event." << endl; wcout << ss.str();// Wait for the event to be set.WaitForSingleObject(hEvent, INFINITE);// Print a message after the event is set.ss = wstringstream();ss << L"\t\tContext " << GetExecutionContextId() << L": received the event." << endl; wcout << ss.str();});}// Wait a sufficient amount of time for all tasks to enter // the waiting state.Sleep(1000L);// Set the event.wstringstream ss;ss << L"\tSetting the event." << endl; wcout << ss.str();SetEvent(hEvent);// Wait for all tasks to complete.tasks.wait();// Close the event handle.CloseHandle(hEvent);
}int wmain()
{// Create a scheduler policy that allows up to two // simultaneous tasks.SchedulerPolicy policy(1, MaxConcurrency, 2);// Attach the policy to the current scheduler.CurrentScheduler::Create(policy);wcout << L"Cooperative event:" << endl;RunCooperativeEvents();wcout << L"Windows event:" << endl;RunWindowsEvents();
}

输出如下:

Cooperative event:Context 0: waiting on an event.Context 1: waiting on an event.Context 2: waiting on an event.Context 3: waiting on an event.Context 4: waiting on an event.Setting the event.Context 5: received the event.Context 6: received the event.Context 7: received the event.Context 8: received the event.Context 9: received the event.
Windows event:Context 10: waiting on an event.Context 11: waiting on an event.Setting the event.Context 12: received the event.Context 14: waiting on an event.Context 15: received the event.Context 16: waiting on an event.Context 17: received the event.Context 18: waiting on an event.Context 19: received the event.Context 13: received the event.

由于 event 类以协作方式运行,因此当事件等待进入信号状态时,调度程序可以将处理资源重新分配给另一个上下文。 因此,更多的工作将由使用 event 类的版本完成。 在使用 Windows 事件的版本中,每个等待任务必须在下一个任务开始之前进入信号状态。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/149752.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

RtspServer:轻量级RTSP服务器和推流器

文章目录 项目概述技术分析支持的编码格式传输方式心跳检测机制RTSP 推流安全性 架构分析RtspServer 整体架构流程分析1. 客户端连接和会话建立2. 媒体数据传输3. 心跳检测和连接维护 xop 基础库项目介绍功能特性xop 整体架构 应用场景社区问题收集与解答问题一&#xff1a;刚开…

【YOLO学习】YOLOv1详解

文章目录 1. 概述2. 算法流程3. 网络结构4. 损失函数 1. 概述 1. YOLO 的全称是 You Only Look Once: Unified, Real-Time Object Detection。YOLOv1 的核心思想就是利用整张图作为网络的输入&#xff0c;直接在输出层回归 bounding box 的位置和 bounding box 所属的类别。简单…

【AI创作组】Matlab中进行符号计算

提示:代码一定要自己运行过才算数…… 1. 符号计算工具箱介绍 1.1 工具箱功能 MATLAB的符号计算工具箱,即Symbolic Math Toolbox,是一套强大的数学软件工具,它使得MATLAB具备了符号运算的能力。该工具箱提供了一系列函数,用于求解、绘制和操作符号数学方程。用户可以直接…

深度学习之表示学习 - 半监督解释因果关系篇

序言 在深度学习的广阔领域中&#xff0c;表示学习&#xff08; Representation Learning \text{Representation Learning} Representation Learning&#xff09;与半监督学习&#xff08; Semi-supervised Learning \text{Semi-supervised Learning} Semi-supervised Learnin…

C++:deque的底层原理

一、deque是是双端队列。 deque(双端队列)&#xff1a;是一种双开口的"连续"空间的数据结构&#xff0c;双开口的含义是&#xff1a;可以在头尾两端进行插入和删除操作&#xff0c;且时间复杂度为O(1)&#xff0c;与vector比较&#xff0c;头插效率高&#xff0c;不…

spring里面内置的非常实用的工具

一 、请求数据记录 Spring Boot提供了一个内置的日志记录解决方案&#xff0c;通过 AbstractRequestLoggingFilter 可以记录请求的详细信息。 AbstractRequestLoggingFilter 有两个不同的实现类&#xff0c;我们常用的是 CommonsRequestLoggingFilter。 通过 CommonsRequestL…

AlDente Pro for Mac电池健康保护工具

AlDente Pro for Mac 是一款适用于 Mac 的实用电池健康保护工具。以下是它的主要特点和优势&#xff1a; 软件下载地址 一、保护电池寿命的原理 锂离子和聚合物电池&#xff08;如 Mac 笔记本中的电池&#xff09;在 30% 到 80% 之间运行时使用寿命最长。始终将电池电量保持…

网关基础知识

1.网关路由 网关&#xff1a;就是网络的关口&#xff0c;负责请求的路由、转发、身份校验。 在SpringCloud中网关的实现包括两种&#xff1a; 1.Spring Cloud Gateway Spring官方出品 基于WebFlux响应式编程 无需调优即可获得优异性能 2.Netflix Zuul Netflix出品 基于Ser…

快递物流短信API接口代码

官网&#xff1a;快递鸟 API参数 用户信息类 一.短信模版 1.接口说明 使用快递鸟短信功能时&#xff0c;预先设置好短信模板和对应的发送规则&#xff0c;快递鸟短信API将根据设置的好的模板和规则&#xff0c;进行短信的发送和反馈。 (1)仅支持Json格式。 (2)请求指令810…

vulnhub(13):Digitalworld.local JOY(ftp 的未授权文件读写漏洞、文件覆盖提权)

端口 nmap主机发现 nmap -sn 192.168.72.0/24 ​ Nmap scan report for 192.168.72.171 Host is up (0.00020s latency). ​ 171是新出现的机器&#xff0c;他就是靶机 nmap端口扫描 nmap -Pn 192.168.72.171 -p- --min-rate 10000 -oA nmap/scan 扫描开放端口保存到 nmap/sca…

Python | Leetcode Python题解之第435题无重叠区间

题目&#xff1a; 题解&#xff1a; class Solution:def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:if not intervals:return 0intervals.sort(keylambda x: x[1])n len(intervals)right intervals[0][1]ans 1for i in range(1, n):if intervals…

网络资源模板--Android Studio 垃圾分类App

目录 一、项目演示 二、项目测试环境 三、项目详情 四、完整的项目源码 一、项目演示 网络资源模板--垃圾分类App 二、项目测试环境 三、项目详情 登陆注册 设置点击监听器&#xff1a;当用户点击注册按钮时触发事件。获取用户输入&#xff1a;从输入框获取用户名和密码&a…

springboot 接口接收及响应xml数据

1.实体类 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;XmlRootElement XmlAccessorType(XmlAccessType.FIELD) // …

【SpringBoot详细教程】-03-整合Junit【持续更新】

JUnit是一个用于Java编程语言的测试框架。它支持自动化单元测试&#xff0c;可以帮助开发人员测试代码的正确性和健壮性。JUnit提供了一组注解、断言和测试运行器&#xff0c;可以方便地编写和运行单元测试。 SpringBoot 整合 junit 特别简单&#xff0c;分为以下三步完成 在…

网络安全等级保护 | 规范企业网络系统安全使用 | 天锐股份助力等保制度落地

在当今数字化高速发展的时代&#xff0c;网络安全对于企业的重要性日益凸显。而近年来&#xff0c;数据泄露、网络攻击等安全事件频发&#xff0c;给企业和个人带来了前所未有的挑战。在这一背景下&#xff0c;网络安全等级保护制度&#xff08;简称“等保”&#xff09;作为国…

经颅磁刺激技术,脑科学研究——精神患者治疗方案

经颅磁刺激&#xff08;Transcranial Magnetic Stimulation &#xff0c;TMS&#xff09;技术是一种利用脉冲磁场作用于中枢神经系统&#xff08;主要是大脑&#xff09;&#xff0c;改变皮层神经细胞的膜电位&#xff0c;使之产生感应电流&#xff0c;影响脑内代谢和神经电活动…

Qt QFileDialog使用方法

头文件 #include <QFileDialog> 成员名称返回值说明getExistingDirectoryQString返回用户选中的文件夹路径getExistingDirectoryUrlQUrl与QFileDialog::getExistingDirectory()的主要区别来自于为用户提供的选择远程目录的能力getOpenFileNameQString返回用户选中的文件…

基于深度学习的树叶识别系统的设计与实现(pyqt5 python3.9 yolov8 10000张数据集)

&#x1f497;博主介绍&#x1f497;&#xff1a;✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌ 温馨提示&#xff1a;文末有 CSDN 平台官方提供的老师 Wechat / QQ 名片 :) Java精品实战案例《700套》 2025最新毕业设计选题推荐…

解锁微软录屏工具:2024 开启屏幕录制新时代

现在快节奏的生活环境&#xff0c;录屏工具已成为不可或缺的记录利器&#xff0c;其应用范围广泛。若你正对windows自带录屏功能充满好奇&#xff0c;渴望掌握其操作方法&#xff0c;或是寻求更多专业、便捷的录屏软件选项&#xff0c;那么就请继续阅读吧。 1.福昕录屏大师 链…

Figma 中要放大并下载 UI 设计中的图标

Figma 中要放大并下载 UI 设计中的图标&#xff0c;通常可以通过以下几步操作来实现&#xff1a; 1. 放大图标&#xff1a; 打开 Figma 文件并找到你想要放大的图标。 选中图标&#xff0c;点击界面右上角的 “缩放”工具&#xff08;放大镜图标&#xff09;&#xff0…