Aracılığıyla paylaş


Nasıl yapılır: Zamanlayıcı Örneğini Yönetme

Zamanlayıcı örnekleri, belirli zamanlama ilkelerini çeşitli iş yükü türleriyle ilişkilendirmenize olanak tanır. Bu konu, zamanlayıcı örneğinin nasıl oluşturulacağını ve yönetileceğini gösteren iki temel örnek içerir.

Örnekler, varsayılan zamanlayıcı ilkelerini kullanan zamanlayıcılar oluşturur. Özel ilke kullanan bir zamanlayıcı oluşturan bir örnek için bkz . Nasıl yapılır: Belirli Zamanlayıcı İlkelerini Belirtme.

Uygulamanızda bir zamanlayıcı örneğini yönetmek için

  1. Zamanlayıcının kullanacağı ilke değerlerini içeren bir eşzamanlılık::SchedulerPolicy nesnesi oluşturun.

  2. Zamanlayıcı örneği oluşturmak için eşzamanlılık::CurrentScheduler::Create yöntemini veya eşzamanlılık::Scheduler::Create yöntemini çağırın.

    yöntemini kullanırsanız, zamanlayıcıyı Scheduler::Creategeçerli bağlamla ilişkilendirmeniz gerektiğinde eşzamanlılık::Scheduler::Attach yöntemini çağırın.

  3. Sinyalsiz, otomatik olarak sıfırlanan bir olay nesnesine tanıtıcı oluşturmak için CreateEvent işlevini çağırın.

  4. Tanıtıcıyı yeni oluşturduğunuz olay nesnesine eşzamanlılık::CurrentScheduler::RegisterShutdownEvent yöntemine veya concurrency::Scheduler::RegisterShutdownEvent yöntemine geçirin. Bu, zamanlayıcı yok edildiğinde ayarlanacak olayı kaydeder.

  5. Geçerli zamanlayıcının zamanlamasını istediğiniz görevleri gerçekleştirin.

  6. Geçerli zamanlayıcıyı ayırmak ve önceki zamanlayıcıyı geçerli zamanlayıcı olarak geri yüklemek için eşzamanlılık::CurrentScheduler::D etach yöntemini çağırın.

    yöntemini kullanırsanız, nesnesinin Scheduler::Createbaşvuru sayısını azaltması için concurrency::Scheduler::Release yöntemini çağırın Scheduler .

  7. Zamanlayıcının kapatılmasını beklemek için tanıtıcıyı olaya WaitForSingleObject işlevine geçirin.

  8. Tanıtıcıyı olay nesnesine kapatmak için CloseHandle işlevini çağırın.

Örnek

Aşağıdaki kod, zamanlayıcı örneğini yönetmenin iki yolunu gösterir. Her örnek, geçerli zamanlayıcının benzersiz tanımlayıcısını yazdıran bir görev gerçekleştirmek için ilk olarak varsayılan zamanlayıcıyı kullanır. Ardından her örnek aynı görevi yeniden gerçekleştirmek için bir zamanlayıcı örneği kullanır. Son olarak, her örnek varsayılan zamanlayıcıyı geçerli zamanlayıcı olarak geri yükler ve görevi bir kez daha gerçekleştirir.

İlk örnek, bir zamanlayıcı örneği oluşturmak ve bunu geçerli bağlamla ilişkilendirmek için eşzamanlılık::CurrentScheduler sınıfını kullanır. İkinci örnek aynı görevi gerçekleştirmek için eşzamanlılık::Scheduler sınıfını kullanır. Genellikle, CurrentScheduler sınıfı geçerli zamanlayıcı ile çalışmak için kullanılır. sınıfını kullanan ikinci örnek, zamanlayıcının Scheduler geçerli bağlamla ne zaman ilişkilendirildiği veya belirli zamanlayıcıları belirli görevlerle ilişkilendirmek istediğinizde yararlıdır.

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

using namespace concurrency;
using namespace std;

// Prints the identifier of the current scheduler to the console.
void perform_task()
{
   // A task group.
   task_group tasks;

   // Run a task in the group. The current scheduler schedules the task.
   tasks.run_and_wait([] { 
      wcout << L"Current scheduler id: " << CurrentScheduler::Id() << endl;
   });
}

// Uses the CurrentScheduler class to manage a scheduler instance.
void current_scheduler()
{
   // Run the task.
   // This prints the identifier of the default scheduler.
   perform_task();

   // For demonstration, create a scheduler object that uses 
   // the default policy values.
   wcout << L"Creating and attaching scheduler..." << endl;
   CurrentScheduler::Create(SchedulerPolicy());

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

   // Run the task again.
   // This prints the identifier of the new scheduler.
   perform_task();

   // Detach the current scheduler. This restores the previous scheduler
   // as the current one.
   wcout << L"Detaching scheduler..." << endl;
   CurrentScheduler::Detach();

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

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

   // Run the sample task again.
   // This prints the identifier of the default scheduler.
   perform_task();
}

// Uses the Scheduler class to manage a scheduler instance.
void explicit_scheduler()
{
   // Run the task.
   // This prints the identifier of the default scheduler.
   perform_task();

   // For demonstration, create a scheduler object that uses 
   // the default policy values.
   wcout << L"Creating scheduler..." << endl;
   Scheduler* scheduler = Scheduler::Create(SchedulerPolicy());

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

   // Associate the scheduler with the current thread.
   wcout << L"Attaching scheduler..." << endl;
   scheduler->Attach();

   // Run the sample task again.
   // This prints the identifier of the new scheduler.
   perform_task();

   // Detach the current scheduler. This restores the previous scheduler
   // as the current one.
   wcout << L"Detaching scheduler..." << endl;
   CurrentScheduler::Detach();

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

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

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

   // Run the sample task again.
   // This prints the identifier of the default scheduler.
   perform_task();
}

int wmain()
{
   // Use the CurrentScheduler class to manage a scheduler instance.
   wcout << L"Using CurrentScheduler class..." << endl << endl;
   current_scheduler();

   wcout << endl << endl;

   // Use the Scheduler class to manage a scheduler instance.
   wcout << L"Using Scheduler class..." << endl << endl;
   explicit_scheduler();
}

Bu örnek aşağıdaki çıkışı oluşturur.

Using CurrentScheduler class...

Current scheduler id: 0
Creating and attaching scheduler...
Current scheduler id: 1
Detaching scheduler...
Current scheduler id: 0

Using Scheduler class...

Current scheduler id: 0
Creating scheduler...
Attaching scheduler...
Current scheduler id: 2
Detaching scheduler...
Current scheduler id: 0

Kod Derleniyor

Örnek kodu kopyalayıp bir Visual Studio projesine yapıştırın veya adlı scheduler-instance.cpp bir dosyaya yapıştırın ve ardından bir Visual Studio Komut İstemi penceresinde aşağıdaki komutu çalıştırın.

cl.exe /EHsc scheduler-instance.cpp

Ayrıca bkz.

Zamanlayıcı Örnekleri
Nasıl yapılır: Belirli Zamanlayıcı İlkeleri Belirtme