Question

John_xl on Tue, 10 Jun 2014 06:29:44


#include "stdafx.h" #include <ppltasks.h> #include <agents.h> #include <iostream> #include <array> #include <string> using namespace std; using namespace concurrency; // Creates a task that completes after the specified delay. task<void> complette_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 // return that continuation task. return event_set.then([callback, fire_once]() { delete callback; delete fire_once; }); } // Cancels the provided task after the specified 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> successs_task = t.then([](T) { return true; }); // Create a task that returns false after the specified timeout. task<bool> failure_task = complette_after(timeout).then([] { return false; }); // Create a continuation task that cancels the overall task // if the timeout task finishes first. return (failure_task || successs_task).then([t, cts](bool success) { if (!success) { cts.cancel(); } 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(); } 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() { //task<array<array<int, 10>, 10>> create_identity_martricx([] //{ // array<array<int, 10>, 10> matrix; // int row = 0; // for_each(begin(matrix), end(matrix), [&row](array<int, 10>& matrixRow) // { // fill(begin(matrixRow), end(matrixRow), 0); // matrixRow[row++] = 1; // }); // return matrix; //}); //auto print_matrix = create_identity_martricx.then([](array<array<int, 10>, 10> matrix) //{ // for_each(begin(matrix), end(matrix), [](array<int, 10>& matrixRow) // { // wstring comma; // for_each(begin(matrixRow), end(matrixRow), [&comma](int n) // { // wcout << comma << n; // comma = L", "; // }); // wcout << endl; // }); //}); //print_matrix.wait(); //unsigned int max = 100000; /* Ok */ unsigned int max = 1000000; /*The complette_after task doesn't finish.*/ unsigned int timeout = 5000; //cout << "Please enter max number and timeout eg(10000, 5000)"; //cout.flush(); //cin >> max >> timeout; bool success = true; do { success = count_primes(max, timeout); timeout /= 2; } while (success); return 0; }


Sponsored



Replies

Hasibur Rahman (MSFT) on Wed, 18 Jun 2014 00:03:23


Hi Mike,

Could you please also describe your problem in words and what is the behavior you are observing?

Thanks