std::try_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... > int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN& lockn... ); |
(desde C++11) | |
Trata de bloquear cada uno de los objetos dado
Lockable lock1, lock2, ..., lockn llamando try_lock en orden comenzando con la primera . Original:
Tries to lock each of the given
Lockable objects lock1, lock2, ..., lockn by calling try_lock in order beginning with the first. 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 una llamada a
try_lock falla, unlock se llama por cualquier objetos bloqueados y un índice de 0 basada en el objeto de que no se pudo bloquear devuelto se .Original:
If a call to
try_lock fails, unlock is called for any locked objects and a 0-based index of the object that failed to lock is returned.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 una llamada a los resultados
try_lock en una excepción, unlock se llama para todos los objetos bloqueados antes de volver a lanzar .Original:
If a call to
try_lock 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
-1 en caso de éxito, o 0 basado en valor de índice del objeto que no pudo bloquear .Original:
-1 on success, or 0-based index value of the object that failed to 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.
[editar] Ejemplo
El siguiente ejemplo utiliza
std::try_lock periódicamente y cuenta Poner contadores a cero que se ejecutan en hilos separados .
Original:
The following example uses
std::try_lock to periodically tally and reset counters running in separate threads.
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 <vector> #include <thread> #include <iostream> #include <functional> #include <chrono> int main() { int foo_count = 0; std::mutex foo_count_mutex; int bar_count = 0; std::mutex bar_count_mutex; int overall_count = 0; bool done = false; std::mutex done_mutex; auto increment = [](int &counter, std::mutex &m, const char *desc) { for (int i = 0; i < 10; ++i) { std::unique_lock<std::mutex> lock(m); ++counter; std::cout << desc << ": " << counter << '\n'; lock.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } }; std::thread increment_foo(increment, std::ref(foo_count), std::ref(foo_count_mutex), "foo"); std::thread increment_bar(increment, std::ref(bar_count), std::ref(bar_count_mutex), "bar"); std::thread update_overall([&]() { done_mutex.lock(); while (!done) { done_mutex.unlock(); int result = std::try_lock(foo_count_mutex, bar_count_mutex); if (result == -1) { overall_count += foo_count + bar_count; foo_count = 0; bar_count = 0; std::cout << "overall: " << overall_count << '\n'; foo_count_mutex.unlock(); bar_count_mutex.unlock(); } std::this_thread::sleep_for(std::chrono::seconds(2)); done_mutex.lock(); } done_mutex.unlock(); }); increment_foo.join(); increment_bar.join(); done_mutex.lock(); done = true; done_mutex.unlock(); update_overall.join(); std::cout << "Done processing\n" << "foo: " << foo_count << '\n' << "bar: " << bar_count << '\n' << "overall: " << overall_count << '\n'; }
Posible salida:
bar: 1 foo: 1 foo: 2 bar: 2 foo: 3 overall: 5 bar: 1 foo: 1 bar: 2 foo: 2 bar: 3 overall: 10 bar: 1 foo: 1 bar: 2 foo: 2 overall: 14 bar: 1 foo: 1 bar: 2 overall: 17 foo: 1 bar: 1 foo: 2 overall: 20 Done processing foo: 0 bar: 0 overall: 20
[editar] Ver también
| (C++11) |
bloquea especificados exclusiones mutuas bloques si hay alguno disponible Original: locks specified mutexes, blocks if any are unavailable 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) |

