Share via


演练:调整现有代码以使用轻量级任务

本主题演示如何调整使用 Windows API 创建和执行线程的现有代码以使用轻量级任务。

轻量级任务是直接从 concurrency::Schedulerconcurrency::ScheduleGroup 对象计划的任务。 当改编现有代码以使用并发运行时的计划功能时,轻量级任务非常有用。

先决条件

在开始本演练之前,请阅读主题任务计划程序

示例

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

初始代码

// windows-threads.cpp
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

#define BUF_SIZE 255

DWORD 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 attributes
      0,                      // use default stack size  
      MyThreadFunction,       // thread function name
      pData,                  // 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>
  1. concurrency 命名空间添加 using 指令。
using namespace concurrency;
  1. 更改 MyThreadFunction 的声明以使用 __cdecl 调用约定并返回 void
void __cdecl MyThreadFunction(LPVOID param);
  1. 修改 MyData 结构以包含向任务已完成的主应用程序发送信号的 concurrency::event 对象。
typedef struct MyData {
    int val1;
    int val2;
    event signal;
} MYDATA, *PMYDATA;
  1. 不调用 CreateThread,改为调用 concurrency::CurrentScheduler::ScheduleTask 方法。
CurrentScheduler::ScheduleTask(MyThreadFunction, pData);
  1. 不调用 WaitForSingleObject,改为调用 concurrency::event::wait 方法以等待任务完成。
// Wait for the task to finish.
pData->signal.wait();
  1. 删除对 CloseHandle 的调用。

  2. 更改 MyThreadFunction 定义的签名以匹配步骤 3。

void __cdecl MyThreadFunction(LPVOID lpParam)
  1. MyThreadFunction 函数的末尾,调用 concurrency::event::set 方法以向任务已完成的主应用程序发送信号。
pData->signal.set();
  1. 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 255

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

另请参阅

任务计划程序
Scheduler 类