std::async
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <future>
|
||
template< class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> |
(1) | (dal C++11) |
template< class Function, class... Args > std::future<typename std::result_of<Function(Args...)>::type> |
(2) | (dal C++11) |
La funzione
1) async
modello esegue la funzione f
in modo asincrono (potenzialmente in un thread separato) e restituisce un std::future che finirà per contenere il risultato di questa chiamata di funzione.Original:
The template function
async
runs the function f
asynchronously (potentially in a separate thread) and returns a std::future that will eventually hold the result of that function call.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Si comporta come async(std::launch::async | std::launch::deferred, f, args...). In altre parole,
2) f
può essere avviato in un nuovo thread o può essere eseguito in modo sincrono quando il std::future risultante viene eseguita una query per un valore.Original:
Behaves the same as async(std::launch::async | std::launch::deferred, f, args...). In other words,
f
may be started in a new thread or it may be run synchronously when the resulting std::future is queried for a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Chiama una funzione con argomenti
f
args
secondo una politica di lancio specifica policy
: Original:
Calls a function
f
with arguments args
according to a specific launch policy policy
: The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Se la bandiera async è impostato (cioè policy & std::launch::async != 0), poi
async
genera un nuovo thread di esecuzione come per std::thread(f, args...), salvo che, se la funzione restituisce un valoref
o genera un'eccezione, viene memorizzato nello stato condiviso accessibile attraverso la std::future cheasync
restituisce al chiamante.Original:If the async flag is set (i.e. policy & std::launch::async != 0), thenasync
spawns a new thread of execution as if by std::thread(f, args...), except that if the functionf
returns a value or throws an exception, it is stored in the shared state accessible through the std::future thatasync
returns to the caller.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se la differita bandiera' è impostato (cioè policy & std::launch::deferred != 0), poi convertiti
async
args...
allo stesso modo dal costruttore std::thread, ma non genera un nuovo thread di esecuzione. Invece, valutazione pigro viene eseguita: la prima chiamata a un non-temporizzato attendere funzione sul std::future cheasync
restituito al chiamante causeràf(args...)
da eseguire nel thread corrente. Tutti gli ulteriori accessi al std::future stesso restituirà immediatamente il risultato.Original:If the deferred flag is set (i.e. policy & std::launch::deferred != 0), thenasync
convertsargs...
the same way as by std::thread constructor, but does not spawn a new thread of execution. Instead, lazy evaluation is performed: the first call to a non-timed wait function on the std::future thatasync
returned to the caller will causef(args...)
to be executed in the current thread. All further accesses to the same std::future will return the result immediately.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se entrambe le bandierine std::launch::async e std::launch::deferred si trovano in
policy
, spetta alla realizzazione se eseguire l'esecuzione asincrona o valutazione pigra.Original:If both the std::launch::async and std::launch::deferred flags are set inpolicy
, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Parametri
f | - | oggetto funzione o la funzione da chiamare
Original: function or function object to call The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
args... | - | parametri da passare al
f Original: parameters to pass to f The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
policy | - | maschera di bit valore, dove i singoli bit controllare i metodi consentiti di esecuzione
Original: bitmask value, where individual bits control the allowed methods of execution
The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[modifica] Valore di ritorno
std::future riferimento al valore di ritorno della funzione.
Original:
std::future referring to the return value of the function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Eccezioni
Lanci std::system_error con std::errc::resource_unavailable_try_again condizione di errore se la politica è std::launch::async lancio e l'implementazione è in grado di avviare un nuovo thread.
Original:
Throws std::system_error with error condition std::errc::resource_unavailable_try_again if the launch policy is std::launch::async and the implementation is unable to start a new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Note
L'implementazione può estendere il comportamento del sovraccarico prima std::async consentendo supplementari (attuazione definito) bit nella politica di lancio di default.
Original:
The implementation may extend the behavior of the first overload of std::async by enabling additional (implementation-defined) bits in the default launch policy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Esempio
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> template <typename RAIter> int parallel_sum(RAIter beg, RAIter end) { typename RAIter::difference_type len = end-beg; if(len < 1000) return std::accumulate(beg, end, 0); RAIter mid = beg + len/2; auto handle = std::async(std::launch::async, parallel_sum<RAIter>, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get(); } int main() { std::vector<int> v(10000, 1); std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n'; }
Output:
The sum is 10000