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();
}