std::unique_lock::lock
Da cppreference.com.
< cpp | thread | unique lock
![]() | 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. |
void lock(); | (dal C++11) | |
Blocca il mutex associato. Chiama efficacemente mutex()->lock().
Original:
Locks the associated mutex. Effectively calls mutex()->lock().
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.
std::system_error viene generata se non c'è mutex associato o se il mutex è già bloccato.
Original:
std::system_error is thrown if there is no associated mutex or if the mutex is already locked.
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.
Indice |
[modifica]Parametri
(Nessuno)
Original:
(none)
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]Valore di ritorno
(Nessuno)
Original:
(none)
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
- Eventuali eccezioni generate dal mutex()->lock()Original:Any exceptions thrown by mutex()->lock()The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se non vi è associato mutex, std::system_error con un codice di errore di std::errc::operation_not_permittedOriginal:If there is no associated mutex, std::system_error with an error code of std::errc::operation_not_permittedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se il mutex è già bloccato, std::system_error con un codice di errore di std::errc::resource_deadlock_would_occurOriginal:If the mutex is already locked, std::system_error with an error code of std::errc::resource_deadlock_would_occurThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Esempio
L'esempio seguente utilizza
lock
di ri-acquisire un mutex che è stato sbloccato . Original:
The following example uses
lock
to re-acquire a mutex that was unlocked. 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 <mutex>#include <thread>#include <iostream>#include <vector>#include <chrono> int main(){int counter =0;std::mutex counter_mutex;std::vector<std::thread> threads; auto worker_task =[&](int id){std::unique_lock<std::mutex> lock(counter_mutex);++counter;std::cout<< id <<", initial counter: "<< counter <<'\n'; lock.unlock(); // don't hold the lock while we simulate an expensive operationstd::this_thread::sleep_for(std::chrono::seconds(1)); lock.lock();++counter;std::cout<< id <<", final counter: "<< counter <<'\n';}; for(int i =0; i <10;++i) threads.emplace_back(worker_task, i); for(auto&thread : threads) thread.join();}
Possible output:
0, initial counter: 1 1, initial counter: 2 2, initial counter: 3 3, initial counter: 4 4, initial counter: 5 5, initial counter: 6 6, initial counter: 7 7, initial counter: 8 8, initial counter: 9 9, initial counter: 10 6, final counter: 11 3, final counter: 12 4, final counter: 13 2, final counter: 14 5, final counter: 15 0, final counter: 16 1, final counter: 17 7, final counter: 18 9, final counter: 19 8, final counter: 20
[modifica]Vedi anche
prova a bloccare il mutex associato, restituisce se il mutex non è disponibile Original: tries to lock the associated mutex, returns if the mutex is not available The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) | |
sblocca il mutex associato Original: unlocks the associated mutex The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (metodo pubblico) |