◐ Shell
clean mode source ↗

std::mutex::try_lock - cppreference.com

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.

Click here for the English version of this page

<metanoindex/>

<tbody> </tbody>

bool try_lock();

(dal C++11)

Tenta di bloccare il mutex. Ritorna immediatamente. Il successo restituisce acquisizione blocco true, altrimenti restituisce false.

Original:

Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true, otherwise returns false.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

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.

Valore di ritorno

true se il blocco è stato acquisito con successo, altrimenti false.

Original:

true if the lock was acquired successfully, otherwise false.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Eccezioni

Esempio

Questo esempio mostra blocco, try_lock e sbloccare in azione

Original:

This example shows lock, try_lock and unlock in action

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <mutex>
	
int main()
{
    std::mutex test;
    if (test.try_lock()==true)
        std::cout << "lock acquired" << std::endl;
    else
        std::cout << "lock not acquired" << std::endl;
    test.unlock();	//now unlock the mutex
    test.lock();	//to lock it again
    if (test.try_lock())  //true can be left out
        std::cout << "lock acquired" << std::endl;
    else
        std::cout << "lock not acquired" << std::endl;
    test.lock(); //and now the finale (a block)
}

Output:

lock acquired
lock not acquired
(program hangs)

Vedi anche

blocca i blocchi mutex, se il mutex non è disponibile

Original:

locks the mutex, blocks 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) [modifica]

sblocca il mutex

Original:

unlocks the 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) [modifica]