mercredi 8 janvier 2020

Is it possible to make std::mutex::try_lock_for wait infinitely?

Is there a way to make std::mutex::try_lock_for wait infinitely?

I have a timeout variable which is used to wait and acquire a resource. However, I want that if the user code of my class has set the timeout to zero then the logic should wait to acquire the resource infinitely.

I know that I can do something like below but I want to know if there is more elegant way to make try_lock_for wait infinitely

std::chrono::seconds timeout = std::chrono::seconds(4)
if (timeout == std::chrono::seconds::zero())
    mutex.lock()
else
    if (mutex.try_lock_for(timeout))
        // Modify the lock protected resource

Aucun commentaire:

Enregistrer un commentaire