std::atomic::compare_exchange_weak, std::atomic::compare_exchange_strong
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. |
<metanoindex/>
<tbody> </tbody>| Elemento definito nell'header <atomic>
|
||
bool compare_exchange_weak( T& expected, T desired, std::memory_order success, std::memory_order failure ); bool compare_exchange_weak( T& expected, T desired, std::memory_order success, std::memory_order failure ) volatile; |
(1) | (dal C++11) |
bool compare_exchange_weak( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ); bool compare_exchange_weak( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ) volatile; |
(2) | (dal C++11) |
bool compare_exchange_strong( T& expected, T desired, std::memory_order success, std::memory_order failure ); bool compare_exchange_strong( T& expected, T desired, std::memory_order success, std::memory_order failure ) volatile; |
(3) | (dal C++11) |
bool compare_exchange_strong( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ); bool compare_exchange_strong( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ) volatile; |
(4) | (dal C++11) |
Atomicamente confronta il valore memorizzato in
*this con il valore puntato da expected, e se questi sono uguali, sostituisce il primo con desired (esegui lettura-modifica-scrittura). In caso contrario, carica il valore reale memorizzato in *this in *expected (esegue l'operazione di carico).Original:
Atomically compares the value stored in
*this with the value pointed to by expected, and if those are equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value stored in *this into *expected (performs load operation).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.
I modelli di memoria per la lettura-modifica-scrittura e le operazioni di carico sono
success e failure rispettivamente. Nella (2) e (4) versioni order viene utilizzato sia per lettura-modifica-scrittura e caricare operazioni, tranne che std::memory_order_release std::memory_order_relaxed e sono utilizzati per l'operazione di carico se order == std::memory_order_acq_rel, o order == std::memory_order_release rispettivamente.Original:
The memory models for the read-modify-write and load operations are
success and failure respectively. In the (2) and (4) versions order is used for both read-modify-write and load operations, except that std::memory_order_release and std::memory_order_relaxed are used for the load operation if order == std::memory_order_acq_rel, or order == std::memory_order_release respectively.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.
Le forme deboli (1-2) delle funzioni possono fallire spurio, che è, agire come se
*this != *expected anche se sono uguali. Quando un confronto e di scambio è in un ciclo, la versione debole produrrà migliori prestazioni su alcune piattaforme. Quando un debole confronto e scambio richiederebbe un ciclo e uno forte non, il forte è preferibile.Original:
The weak forms (1-2) of the functions are allowed to fail spuriously, that is, act as if
*this != *expected even if they are equal. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.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.
Parametri
| expected | - | puntatore al valore atteso per essere trovato in oggetto atomico
Original: pointer to the value expected to be found in the atomic object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| desired | - | il valore da memorizzare nell'oggetto atomico se è come previsto
Original: the value to store in the atomic object if it is as expected The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| success | - | la sincronizzazione memoria ordinamento per la lettura-modifica-scrittura, se il confronto ha esito positivo. Tutti i valori sono consentiti .
Original: the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| failure | - | la sincronizzazione della memoria di ordinazione per l'operazione di caricamento, se il paragone non regge. Non può essere std::memory_order_release o
std::memory_order_ack_rel e non può specificare più forte ordine di successOriginal: the memory synchronization ordering for the load operation if the comparison fails. Cannot be std::memory_order_release or std::memory_order_ack_rel and cannot specify stronger ordering than successThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| order | - | la sincronizzazione memoria ordinamento per entrambe le operazioni
Original: the memory synchronization ordering for both operations The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Valore di ritorno
true se il valore sottostante atomico è stato modificato, false altrimenti.Original:
true if the underlying atomic value was changed, false otherwise.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.
Eccezioni
Esempio
Viene illustrato come compare_exchange_strong né cambia il valore della variabile atomico o la variabile utilizzata per il confronto .
Original:
Demonstrates how compare_exchange_strong either changes the value of the atomic variable or the variable used for comparison.
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.
#include <atomic>
#include <iostream>
std::atomic<int> ai;
int tst_val= 4;
int new_val= 5;
bool exchanged= false;
void valsout()
{
std::cout << "ai= " << ai
<< " tst_val= " << tst_val
<< " new_val= " << new_val
<< " exchanged= " << std::boolalpha << exchanged
<< "\n";
}
int main()
{
ai= 3;
valsout();
// tst_val != ai ==> tst_val is modified
exchanged= ai.compare_exchange_strong( tst_val, new_val );
valsout();
// tst_val == ai ==> ai is modified
exchanged= ai.compare_exchange_strong( tst_val, new_val );
valsout();
return 0;
}
Output:
ai= 3 tst_val= 4 new_val= 5 exchanged= false
ai= 3 tst_val= 3 new_val= 5 exchanged= false
ai= 5 tst_val= 3 new_val= 5 exchanged= true
Vedi anche
confronta atomicamente il valore dell'oggetto atomica con non-atomica argomento ed esegue lo scambio atomico se il carico uguale o atomico in caso contrario Original: atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |