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

windows C++-轻量级任务

轻量级任务是直接从 concurrency::Scheduler 或 concurrency::ScheduleGroup 对象计划的任务。 当改编现有代码以使用并发运行时的计划功能时,轻量级任务非常有用。本文演示如何调整使用 Windows API 创建和执行线程的现有代码以使用轻量级任务。

示例

以下示例演示了 Windows API 创建和执行线程的典型用法。 此示例使用 CreateThread 函数在单独的线程上调用 MyThreadFunction。

初始代码
// windows-threads.cpp
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>#define BUF_SIZE 255DWORD WINAPI MyThreadFunction(LPVOID param);// Data structure for threads to use.
typedef struct MyData {int val1;int val2;
} MYDATA, *PMYDATA;int _tmain()
{// Allocate memory for thread data.PMYDATA pData = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));if( pData == NULL ){ExitProcess(2);}// Set the values of the thread data.pData->val1 = 50;pData->val2 = 100;// Create the thread to begin execution on its own.DWORD dwThreadId;HANDLE hThread = CreateThread( NULL,                   // default security attributes0,                      // use default stack size  MyThreadFunction,       // thread function namepData,                  // argument to thread function 0,                      // use default creation flags &dwThreadId);           // returns the thread identifier if (hThread == NULL) {      ExitProcess(3);}// Wait for the thread to finish.WaitForSingleObject(hThread, INFINITE);// Close the thread handle and free memory allocation.CloseHandle(hThread);HeapFree(GetProcessHeap(), 0, pData);return 0;
}DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{PMYDATA pData = (PMYDATA)lpParam;// Use thread-safe functions to print the parameter values.TCHAR msgBuf[BUF_SIZE];StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pData->val1, pData->val2); size_t cchStringSize;StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);DWORD dwChars;WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), msgBuf, (DWORD)cchStringSize, &dwChars, NULL);return 0;
}

本示例生成以下输出。

Parameters = 50, 100
调整示例以使用轻量级任务
// 1. 为头文件 concrt.h 添加 #include 指令。
#include <concrt.h>// 2. 为 concurrency 命名空间添加 using 指令。
using namespace concurrency;// 3. 更改 MyThreadFunction 的声明以使用 __cdecl 调用约定并返回 void。
void __cdecl MyThreadFunction(LPVOID param);// 4. 修改 MyData 结构以包含向任务已完成的主应用程序发送信号的 concurrency::event 对象。
typedef struct MyData {int val1;int val2;event signal;
} MYDATA, *PMYDATA;// 5. 不调用 CreateThread,改为调用 concurrency::CurrentScheduler::ScheduleTask 方法。
CurrentScheduler::ScheduleTask(MyThreadFunction, pData);// 6. 不调用 WaitForSingleObject,改为调用 concurrency::event::wait 方法以等待任务完成。
// Wait for the task to finish.
pData->signal.wait();// 删除对 CloseHandle 的调用。// 7. 更改 MyThreadFunction 定义的签名以匹配步骤 3。
void __cdecl MyThreadFunction(LPVOID lpParam)// 8.在 MyThreadFunction 函数的末尾,调用 concurrency::event::set 
// 方法以向任务已完成的主应用程序发送信号。
pData->signal.set();// 从 MyThreadFunction 中删除 return 语句。
完成的代码

以下已完成的示例演示了使用轻量级任务调用 MyThreadFunction 函数的代码。

// migration-lwt.cpp
// compile with: /EHsc
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <concrt.h>using namespace concurrency;#define BUF_SIZE 255void __cdecl MyThreadFunction(LPVOID param);// Data structure for threads to use.
typedef struct MyData {int val1;int val2;event signal;
} MYDATA, *PMYDATA;int _tmain()
{// Allocate memory for thread data.PMYDATA pData = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));if( pData == NULL ){ExitProcess(2);}// Set the values of the thread data.pData->val1 = 50;pData->val2 = 100;// Create the thread to begin execution on its own.CurrentScheduler::ScheduleTask(MyThreadFunction, pData);// Wait for the task to finish.pData->signal.wait();// Free memory allocation.HeapFree(GetProcessHeap(), 0, pData);return 0;
}void __cdecl MyThreadFunction(LPVOID lpParam)
{PMYDATA pData = (PMYDATA)lpParam;// Use thread-safe functions to print the parameter values.TCHAR msgBuf[BUF_SIZE];StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pData->val1, pData->val2); size_t cchStringSize;StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);DWORD dwChars;WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), msgBuf, (DWORD)cchStringSize, &dwChars, NULL);pData->signal.set();
}

http://www.mrgr.cn/news/48988.html

相关文章:

  • 大模型生图安全疫苗注入赛题解析(DataWhale组队学习)
  • R语言统计分析——折线图
  • 自注意力机制self-attention中QKV矩阵的含义
  • 2025届保研-优营率0%上岸C9
  • C++面试速通宝典——24
  • 基于SpringBoot+Vue的陶怡居茶铺管理系统设计实现(协同过滤算法)
  • 实践体验密集小目标检测,以小麦麦穗颗粒为基准,基于嵌入式端超轻量级模型LeYOLO全系列【n/s/m/l】参数模型开发构建智能精准麦穗颗粒检测计数系统
  • 231水果滑块喜+1
  • g_strdup_printf
  • PostgreSql的备份和升级
  • 自定义注解和组件扫描在Spring Boot中动态注册Bean(二)
  • QEMU与KVM架构
  • 百度搜索引擎是如何解决用户点击率与网站排名关联度的呢?
  • 【ShuQiHere】K近邻算法(KNN)全面解析:从理论到实现
  • [Python]如何在Ubuntu中建置python venv虛擬環境,並安裝TensorFlow和OpenCV函式庫?
  • 1129.统计数字字符个数(vs2022中 gets, gets_s 无法使用的情况下)
  • 小红书运营教程04(爆款账号分解步骤)
  • 2-SAT 问题详解:逻辑约束与图论的结合
  • 禁用微软的windos安全中心
  • Javascript笔试题目(三)