Condividi tramite


Procedura: creare un'attività che viene completata dopo un ritardo

In questo esempio viene illustrato come utilizzare concurrency::task, concurrency::cancellation_token_source, concurrency::cancellation_token, concurrency::task_completion_event, concurrency::timere le classi di concurrency::call per creare un'attività che completa dopo un ritardo.È possibile utilizzare questo metodo per compilare i cicli che talvolta polling dei dati, inserire gli intervalli, gestione di ritardo di input per un tempo predeterminato, e così via.

Esempio

Nell'esempio seguente vengono illustrate le funzioni complete_after e cancel_after_timeout.La funzione di complete_after crea un oggetto di task completata dopo che il ritardo specificato.Utilizza un oggetto di timer e un oggetto di call per impostare un oggetto di task_completion_event dopo che il ritardo specificato.Utilizzando la classe di task_completion_event, è possibile definire un'attività che completa dopo che un thread o un'altra attività segnala che un valore è disponibile.Quando viene impostato, le attività del listener completate e le continuazioni vengono fornite eseguire.

SuggerimentoSuggerimento

Per ulteriori informazioni sulle classi di call e di timer, che fa parte della libreria di agenti asincroni, vedere Blocchi dei messaggi asincroni.

Le compilazioni di funzione di cancel_after_timeout su complete_after funzionano per annullare un'attività se tale attività non termina prima di un intervallo specificato.La funzione di cancel_after_timeout crea due attività.La prima attività indica l'esito positivo e completa il termine dell'attività fornita completa, la seconda attività indica un errore e viene terminato all'intervallo specificato.La funzione di cancel_after_timeout crea un'attività di continuazione che viene eseguita quando l'attività di esito positivo o negativo.Se l'attività degli errori completa innanzitutto, la continuazione dell'origine di token per annullare l'intera attività.

// Creates a task that completes after the specified delay.
task<void> complete_after(unsigned int timeout)
{
    // A task completion event that is set when a timer fires.
    task_completion_event<void> tce;

    // Create a non-repeating timer.
    auto fire_once = new timer<int>(timeout, 0, nullptr, false);
    // Create a call object that sets the completion event after the timer fires.
    auto callback = new call<int>([tce](int)
    {
        tce.set();
    });

    // Connect the timer to the callback and start the timer.
    fire_once->link_target(callback);
    fire_once->start();

    // Create a task that completes after the completion event is set.
    task<void> event_set(tce);

    // Create a continuation task that cleans up resources and
    // and return that continuation task.
    return event_set.then([callback, fire_once]()
    {
        delete callback;
        delete fire_once;
    });
}

// Cancels the provided task after the specifed delay, if the task
// did not complete.
template<typename T>
task<T> cancel_after_timeout(task<T> t, cancellation_token_source cts, unsigned int timeout)
{
    // Create a task that returns true after the specified task completes.
    task<bool> success_task = t.then([](T)
    {
        return true;
    });
    // Create a task that returns false after the specified timeout.
    task<bool> failure_task = complete_after(timeout).then([]
    {
        return false;
    });

    // Create a continuation task that cancels the overall task 
    // if the timeout task finishes first.
    return (failure_task || success_task).then([t, cts](bool success)
    {
        if(!success)
        {
            // Set the cancellation token. The task that is passed as the
            // t parameter should respond to the cancellation and stop
            // as soon as it can.
            cts.cancel();
        }

        // Return the original task.
        return t;
    });
}

Nell'esempio seguente viene calcolato il conteggio dei numeri primi più volteintervallo [0, 100000].L'operazione ha esito negativo se non viene completato in un limite di tempo specificato.La funzione di count_primes viene illustrato come utilizzare la funzione di cancel_after_timeout.Calcola il numero di innesca nell'intervallo e in non riuscire specificati se l'attività non termina nel tempo specificato.Le chiamate di funzione di wmain ripetuta di funzione di count_primes.Ogni volta, divisa in due il limite di tempo.Il programma completo dopo l'operazione non termina nel limite di tempo corrente.

// Determines whether the input value is prime.
bool is_prime(int n)
{
    if (n < 2)
        return false;
    for (int i = 2; i < n; ++i)
    {
        if ((n % i) == 0)
            return false;
    }
    return true;
}

// Counts the number of primes in the range [0, max_value].
// The operation fails if it exceeds the specified timeout.
bool count_primes(unsigned int max_value, unsigned int timeout)
{
    cancellation_token_source cts;

    // Create a task that computes the count of prime numbers.
    // The task is canceled after the specified timeout.
    auto t = cancel_after_timeout(task<size_t>([max_value, timeout]
    {
        combinable<size_t> counts;
        parallel_for<unsigned int>(0, max_value + 1, [&counts](unsigned int n) 
        {
            // Respond if the overall task is cancelled by canceling 
            // the current task.
            if (is_task_cancellation_requested())
            {
                cancel_current_task();
            }
            // NOTE: You can replace the calls to is_task_cancellation_requested
            // and cancel_current_task with a call to interruption_point.
            // interruption_point();

            // Increment the local counter if the value is prime.
            if (is_prime(n))
            {
                counts.local()++;
            }
        });
        // Return the sum of counts across all threads.
        return counts.combine(plus<size_t>());
    }, cts.get_token()), cts, timeout);

    // Print the result.
    try
    {
        auto primes = t.get();
        wcout << L"Found " << primes << L" prime numbers within " 
              << timeout << L" ms." << endl;
        return true;
    }
    catch (const task_canceled& e)
    {
        wcout << L"The task timed out." << endl;
        return false;
    }
}

int wmain()
{
    // Compute the count of prime numbers in the range [0, 100000] 
    // until the operation fails.
    // Each time the test succeeds, the time limit is halved.

    unsigned int max = 100000;
    unsigned int timeout = 5000;

    bool success = true;
    do
    {
        success = count_primes(max, timeout);
        timeout /= 2;
    } while (success);
}
/* Sample output:
    Found 9592 prime numbers within 5000 ms.
    Found 9592 prime numbers within 2500 ms.
    Found 9592 prime numbers within 1250 ms.
    Found 9592 prime numbers within 625 ms.
    The task timed out.
*/

Quando si utilizza questa tecnica per annullare le attività dopo che un ritardo, alcune attività unstarted non verrà avviato dopo che viene annullata l'intera attività.Tuttavia, è importante per tutte le attività di lunga durata rispondere in modo tempestivo all'annullamento.In questo esempio, le chiamate al metodo di count_primes le funzioni di cancel_current_task e di concurrency::is_task_cancellation_requested rispondere all'annullamento.In alternativa, è possibile chiamare la funzione di concurrency::interruption_point ).Per ulteriori informazioni sull'annullamento di attività, vedere Annullamento nella libreria PPL.

Di seguito è illustrato il codice completo di questo esempio:

// task-delay.cpp
// compile with: /EHsc
#include <ppltasks.h>
#include <agents.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Creates a task that completes after the specified delay.
task<void> complete_after(unsigned int timeout)
{
    // A task completion event that is set when a timer fires.
    task_completion_event<void> tce;

    // Create a non-repeating timer.
    auto fire_once = new timer<int>(timeout, 0, nullptr, false);
    // Create a call object that sets the completion event after the timer fires.
    auto callback = new call<int>([tce](int)
    {
        tce.set();
    });

    // Connect the timer to the callback and start the timer.
    fire_once->link_target(callback);
    fire_once->start();

    // Create a task that completes after the completion event is set.
    task<void> event_set(tce);

    // Create a continuation task that cleans up resources and
    // and return that continuation task.
    return event_set.then([callback, fire_once]()
    {
        delete callback;
        delete fire_once;
    });
}

// Cancels the provided task after the specifed delay, if the task
// did not complete.
template<typename T>
task<T> cancel_after_timeout(task<T> t, cancellation_token_source cts, unsigned int timeout)
{
    // Create a task that returns true after the specified task completes.
    task<bool> success_task = t.then([](T)
    {
        return true;
    });
    // Create a task that returns false after the specified timeout.
    task<bool> failure_task = complete_after(timeout).then([]
    {
        return false;
    });

    // Create a continuation task that cancels the overall task 
    // if the timeout task finishes first.
    return (failure_task || success_task).then([t, cts](bool success)
    {
        if(!success)
        {
            // Set the cancellation token. The task that is passed as the
            // t parameter should respond to the cancellation and stop
            // as soon as it can.
            cts.cancel();
        }

        // Return the original task.
        return t;
    });
}

// Determines whether the input value is prime.
bool is_prime(int n)
{
    if (n < 2)
        return false;
    for (int i = 2; i < n; ++i)
    {
        if ((n % i) == 0)
            return false;
    }
    return true;
}

// Counts the number of primes in the range [0, max_value].
// The operation fails if it exceeds the specified timeout.
bool count_primes(unsigned int max_value, unsigned int timeout)
{
    cancellation_token_source cts;

    // Create a task that computes the count of prime numbers.
    // The task is canceled after the specified timeout.
    auto t = cancel_after_timeout(task<size_t>([max_value, timeout]
    {
        combinable<size_t> counts;
        parallel_for<unsigned int>(0, max_value + 1, [&counts](unsigned int n) 
        {
            // Respond if the overall task is cancelled by canceling 
            // the current task.
            if (is_task_cancellation_requested())
            {
                cancel_current_task();
            }
            // NOTE: You can replace the calls to is_task_cancellation_requested
            // and cancel_current_task with a call to interruption_point.
            // interruption_point();

            // Increment the local counter if the value is prime.
            if (is_prime(n))
            {
                counts.local()++;
            }
        });
        // Return the sum of counts across all threads.
        return counts.combine(plus<size_t>());
    }, cts.get_token()), cts, timeout);

    // Print the result.
    try
    {
        auto primes = t.get();
        wcout << L"Found " << primes << L" prime numbers within " 
              << timeout << L" ms." << endl;
        return true;
    }
    catch (const task_canceled& e)
    {
        wcout << L"The task timed out." << endl;
        return false;
    }
}

int wmain()
{
    // Compute the count of prime numbers in the range [0, 100000] 
    // until the operation fails.
    // Each time the test succeeds, the time limit is halved.

    unsigned int max = 100000;
    unsigned int timeout = 5000;

    bool success = true;
    do
    {
        success = count_primes(max, timeout);
        timeout /= 2;
    } while (success);
}
/* Sample output:
    Found 9592 prime numbers within 5000 ms.
    Found 9592 prime numbers within 2500 ms.
    Found 9592 prime numbers within 1250 ms.
    Found 9592 prime numbers within 625 ms.
    The task timed out.
*/

Compilazione del codice

Per compilare il codice, copiarlo e quindi incollarlo in un progetto di Visual Studio, oppure incollarlo in un file denominato task-delay.cpp quindi viene eseguito il comando seguente in una finestra del prompt dei comandi di Visual Studio.

cl.exe /EHsc task-delay.cpp

Vedere anche

Riferimenti

Classe task (runtime di concorrenza)

Classe cancellation_token_source

Classe cancellation_token

Classe task_completion_event

Funzione is_task_cancellation_requested

Funzione cancel_current_task

Funzione interruption_point

Classe timer

Classe call

Concetti

Parallelismo delle attività (runtime di concorrenza)

Blocchi dei messaggi asincroni

Annullamento nella libreria PPL