std::lock
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Definido en la cabecera <mutex>
|
||
| template< class Lockable1, class Lockable2, class LockableN... > void lock( Lockable1& lock1, Lockable2& lock2, LockableN& lockn... ); |
(desde C++11) | |
Bloquea el dado
Lockable objetos lock1, lock2, ..., lockn utilizando un algoritmo de evitación de interbloqueo para evitar el bloqueo .Original:
Locks the given
Lockable objects lock1, lock2, ..., lockn using a deadlock avoidance algorithm to avoid deadlock.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.
Los objetos están bloqueados por una serie indeterminada de llamadas a
lock, try_lock, unlock. Si una llamada a lock o unlock produce una excepción, se llama unlock para los objetos bloqueados antes de volver a lanzar .Original:
The objects are locked by an unspecified series of calls to
lock, try_lock, unlock. If a call to lock or unlock results in an exception, unlock is called for any locked objects before rethrowing.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.
Contenido |
[editar] Parámetros
| lock1, lock2, ... , lockn | - | el
Lockable objetos que desea bloquearOriginal: the Lockable objects to lockThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
(Ninguno)
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.
[editar] Ejemplo
En el ejemplo siguiente, se utiliza para bloquear
std::lock parejas de semáforos sin punto muerto .
Original:
The following example uses
std::lock to lock pairs of mutexes without deadlock.
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 <functional> #include <chrono> struct Employee { Employee(int id) : id(id) {} int id; std::vector<int> lunch_partners; std::mutex m; }; void send_mail(Employee &e1, Employee &e2) { // simulate a time-consuming messaging operation std::this_thread::sleep_for(std::chrono::seconds(1)); } void assign_lunch_partner(Employee &e1, Employee &e2) { // use std::lock to acquire two locks without worrying about // other calls to assign_lunch_partner deadlocking us std::lock(e1.m, e2.m); e1.lunch_partners.push_back(e2.id); e2.lunch_partners.push_back(e1.id); e1.m.unlock(); e2.m.unlock(); send_mail(e1, e2); send_mail(e2, e1); } int main() { Employee alice(0), bob(1), christina(2), dave(3); // assign in parallel threads because mailing users about lunch assignments // takes a long time std::vector<std::thread> threads; threads.emplace_back(assign_lunch_partner, std::ref(alice), std::ref(bob)); threads.emplace_back(assign_lunch_partner, std::ref(christina), std::ref(bob)); threads.emplace_back(assign_lunch_partner, std::ref(christina), std::ref(alice)); threads.emplace_back(assign_lunch_partner, std::ref(dave), std::ref(bob)); for (auto &thread : threads) thread.join(); }
[editar] Ver también
| (C++11) |
las gestiones para obtener la propiedad de las exclusiones mutuas a través de llamadas repetidas a try_lock Original: attempts to obtain ownership of mutexes via repeated calls to try_lock The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de función) |

