分享方式:


如何:指定特定排程器原則

排程器原則可讓您控制排程器在管理工作時所使用的策略。 本主題說明如何使用排程器原則來增加將進度指示器列印至主控台之工作的執行緒優先順序。

如需搭配非同步代理程式使用自訂排程器原則的範例,請參閱 如何:建立使用特定排程器原則 的代理程式。

範例

下列範例會平行執行兩個工作。 第一個工作會計算第 n Fibonacci 數位。 第二項工作會將進度指示器列印至主控台。

第一個工作會使用遞迴分解來計算 Fibonacci 數位。 也就是說,每個工作會以遞迴方式建立子工作來計算整體結果。 使用遞迴分解的工作可能會使用所有可用的資源,因而耗盡其他工作。 在此範例中,列印進度指示器的工作可能無法及時存取計算資源。

為了提供列印計算資源之進度訊息公平存取的工作,此範例會使用如何:管理排程器實例 中所述 的步驟,建立具有自訂原則的排程器實例。 自訂原則會將執行緒優先順序指定為最高優先順序類別。

此範例會使用 並行::call concurrency::timer 類別來列印進度指示器。 這些類別具有其建構函式的版本,這些建構函式會參考 排程它們的並行::Scheduler 物件。 此範例會使用預設排程器來排程計算 Fibonacci 數位和排程器實例的工作,以排程列印進度指示器的工作。

為了說明使用具有自訂原則的排程器的優點,此範例會執行整體工作兩次。 此範例會先使用預設排程器來排程這兩個工作。 然後,此範例會使用預設排程器來排程第一個工作,以及具有自訂原則的排程器來排程第二個工作。

// scheduler-policy.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <agents.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Computes the nth Fibonacci number.
// This function illustrates a lengthy operation and is therefore
// not optimized for performance.
int fibonacci(int n)
{
   if (n < 2)
      return n;

   // Compute the components in parallel.
   int n1, n2;
   parallel_invoke(
      [n,&n1] { n1 = fibonacci(n-1); },
      [n,&n2] { n2 = fibonacci(n-2); }
   );
  
   return n1 + n2;
}

// Prints a progress indicator while computing the nth Fibonacci number.
void fibonacci_with_progress(Scheduler& progress_scheduler, int n)
{
   // Use a task group to compute the Fibonacci number.
   // The tasks in this group are scheduled by the current scheduler.
   structured_task_group tasks;

   auto task = make_task([n] {
      fibonacci(n);
   });
   tasks.run(task);

   // Create a call object that prints its input to the console.
   // This example uses the provided scheduler to schedule the 
   // task that the call object performs.
   call<wchar_t> c(progress_scheduler, [](wchar_t c) { 
      wcout << c; 
   });

   // Connect the call object to a timer object. The timer object
   // sends a progress message to the call object every 100 ms.
   // This example also uses the provided scheduler to schedule the 
   // task that the timer object performs.
   timer<wchar_t> t(progress_scheduler, 100, L'.', &c, true);
   t.start();

   // Wait for the task that computes the Fibonacci number to finish.
   tasks.wait();

   // Stop the timer.
   t.stop();

   wcout << L"done" << endl;
}

int wmain()
{  
   // Calculate the 38th Fibonacci number.
   const int n = 38;

   // Use the default scheduler to schedule the progress indicator while 
   // the Fibonacci number is calculated in the background.

   wcout << L"Default scheduler:" << endl;
   fibonacci_with_progress(*CurrentScheduler::Get(), n);

   // Now use a scheduler that has a custom policy for the progress indicator.
   // The custom policy specifies the thread priority to the highest 
   // priority class.
   
   SchedulerPolicy policy(1, ContextPriority, THREAD_PRIORITY_HIGHEST);
   Scheduler* scheduler = Scheduler::Create(policy);

   // Register to be notified when the scheduler shuts down.
   HANDLE hShutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
   scheduler->RegisterShutdownEvent(hShutdownEvent);

   wcout << L"Scheduler that has a custom policy:" << endl;
   fibonacci_with_progress(*scheduler, n);

   // Release the final reference to the scheduler. This causes the scheduler
   // to shut down.
   scheduler->Release();

   // Wait for the scheduler to shut down and destroy itself.
   WaitForSingleObject(hShutdownEvent, INFINITE);

   // Close the event handle.
   CloseHandle(hShutdownEvent);
}

此範例會產生下列輸出。

Default scheduler:
...........................................................................done
Scheduler that has a custom policy:
...........................................................................done

雖然這兩組工作會產生相同的結果,但使用自訂原則的版本可讓列印進度指示器的工作以提升優先順序執行,使其表現更能回應。

編譯程式碼

複製範例程式碼,並將其貼到 Visual Studio 專案中,或貼到名為 scheduler-policy.cpp 的檔案中,然後在 Visual Studio 命令提示字元視窗中執行下列命令。

cl.exe /EHsc scheduler-policy.cpp

另請參閱

排程器原則
如何:管理排程器執行個體
如何:建立使用特定排程器原則的代理程式