Aracılığıyla paylaş


Nasıl yapılır: bir Zamanlayıcı örneğini yönetmek

Zamanlayıcı örnekleri belirli planlama ilkeleri, çeşitli iş yükleri ile ilişkilendirmek sağlar.Bu konuda iki temel oluşturmak ve yönetmek bir Zamanlayıcı örneği göstermektedir örnekleri içerir.

Örnekler, varsayılan Zamanlayıcı ilkeleri kullanma planlayıcılar oluşturun.Bir Zamanlayıcı yaratan örnek özel bir ilke kullanır, bakın Nasıl yapılır: belirli Zamanlayıcı ilkeler belirleme.

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

  1. Oluşturma bir concurrency::SchedulerPolicy İlkesi içeren nesne değerleri Zamanlayıcı kullanmak.

  2. Call concurrency::CurrentScheduler::Create yöntemini veya concurrency::Scheduler::Create yöntemi bir Zamanlayıcı örneğini oluşturmak için.

    Kullanırsanız, Scheduler::Create yöntemi, çağrı concurrency::Scheduler::Attach yöntemi geçerli içerikle Zamanlayıcı ilişkilendirmek istediğinizde.

  3. Call CreateEvent erdiği, otomatik sıfırlama olay nesnesi için tanıtıcı oluşturmak için işlev.

  4. Oluşturduğunuz için olay nesnesi için tanıtıcı geçmesi concurrency::CurrentScheduler::RegisterShutdownEvent yöntemini veya concurrency::Scheduler::RegisterShutdownEvent yöntemi.Bu Zamanlayıcı edildiğinde ayarlamak için olay kaydeder.

  5. Zamanlamak için geçerli çizelgeleyicinin çalıştırmasını istediğiniz görevleri gerçekleştirin.

  6. Call concurrency::CurrentScheduler::Detach geçerli Zamanlayıcı ayırmak ve önceki Zamanlayıcısı geçerli olarak geri yüklemek için yöntem.

    Kullanırsanız, Scheduler::Create yöntemi, çağrı concurrency::Scheduler::Release başvuru sayısı azaltma yöntemini Scheduler nesnesi.

  7. Olay işleyicisi aktarmak WaitForSingleObject işlevinin Zamanlayıcı kapanması bekleyin.

  8. Call CloseHandle işlevi, olay nesnesi için Tanıtıcı kapatılamıyor.

Örnek

Aşağıdaki kod, Zamanlayıcı örneğini yönetmek için iki yol gösterir.Her örnek, varsayılan Zamanlayıcı ilk geçerli Zamanlayıcı benzersiz tanımlayıcı yazdırır bir görevi gerçekleştirmek için kullanır.Her örnek, Zamanlayıcı örneği daha sonra yeniden aynı görevi gerçekleştirmek için kullanır.Son olarak, her örnek olarak geçerli bir varsayılan Zamanlayıcı geri yükler ve bir kez daha görevi gerçekleştirir.

İlk örnek concurrency::CurrentScheduler sınıf Zamanlayıcısı örneğini oluşturabilir ve geçerli içerikle ilişkilendirir.İkinci örnek concurrency::Scheduler aynı görevi gerçekleştirmek için sınıf.Genellikle, CurrentScheduler sınıf geçerli Zamanlayıcı ile çalışmak için kullanılır.Kullandığı ikinci örnek Scheduler sınıfı, Zamanlayıcı geçerli içerikle ilişkili olduğunda veya belirli planlayıcılar belirli görevler ile ilişkilendirmek istediğiniz zaman kontrol etmek 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 çıktıyı üretir.

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ın ve Visual Studio Project'te yapıştırın veya adlı bir dosyaya yapıştırın Zamanlayıcı instance.cpp ve Visual Studio komut istemi penceresinde aşağıdaki komutu çalıştırın.

cl.exe /EHsc scheduler-instance.cpp

Ayrıca bkz.

Görevler

Nasıl yapılır: belirli Zamanlayıcı ilkeler belirleme

Kavramlar

Zamanlayıcı örnekleri