What is the main difference between these two Functions?

Advertisement

std::mutex::lock()
std::mutex::try_lock()

  • lock() has a higher privilege over try_lock(). This means that you have a better chance of acquiring a mutex with lock().
  • Both attempt to acquire a lock, but lock() blocks if the mutex is not available, whereas try_lock() returns whether the mutex is available or not.
  • lock() enforces preemption, whereas try_lock() suggests preemption.
  • If the mutex is not available, try_lock() returns with a corresponding code, whereas lock() snatches the mutex from the thread that currently has it.
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Leave a Comment

Share via
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Send this to a friend