Share via


逐步解說:改寫現有程式碼以使用輕量型工作

本主題說明如何調整使用 Windows API 來建立和執行執行緒以使用輕量型工作的現有程式碼。

量型工作 是您直接從 並行::Schedulerency::Scheduler concurrency::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. #include新增標頭檔 concrt.h 的指示詞。
#include <concrt.h>
  1. using新增 命名空間的 concurrency 指示詞。
using namespace concurrency;
  1. 將 的宣告 MyThreadFunction 變更為使用 __cdecl 呼叫慣例,並傳回 void
void __cdecl MyThreadFunction(LPVOID param);
  1. 修改 結構以 MyData 包含 並行::event 物件,此物件會向工作已完成的主要應用程式發出訊號。
typedef struct MyData {
    int val1;
    int val2;
    event signal;
} MYDATA, *PMYDATA;
  1. 將 的呼叫 CreateThread 取代為對 concurrency::CurrentScheduler::ScheduleTask 方法的 呼叫。
CurrentScheduler::ScheduleTask(MyThreadFunction, pData);
  1. 將 的呼叫取代為 對 concurrency::event::wait 方法的呼叫 WaitForSingleObject ,以等候工作完成。
// 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. returnMyThreadFunction 移除 語句。

已完成的程式碼

下列已完成的範例顯示使用輕量型工作呼叫函式的程式 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 類別