Is std::lock_guard exception-safe?

In this scenario:
1
2
3
4
5
6
try {
    std::lock_guard<std::mutex> guard(mutex_defined_somewhere_else);
    return map_accessible_in_another_thread[index]; // Exception could be thrown here.
} catch (std::out_of_range) {
    throw more_informative_exception_type("Requested resource hasn't been loaded yet");
}

Will the mutex be unlocked if an exception is thrown before the end of the try statement?

[edit]Note: In case it wasn't obvious, the mutex locks access to the std::map being indexed into.[/edit]
Last edited on
Yes.

To understand when a destructor is called: http://en.cppreference.com/w/cpp/language/destructor

What the destructor of std::lock_guard does:
http://en.cppreference.com/w/cpp/thread/lock_guard/~lock_guard
I thought so.

Thanks.
Topic archived. No new replies allowed.