0

I have the following codes. I wonder that whether the scoped lock got locked at the beginning of function "do_something"? Thanks very much!

class Test {
public:
    void do_something () {
        std::cout << "anything printable" << std::endl;
        // do something else
        // Has mu_ got locked here?
        ...

        std::lock_guard<std::mutex> l(mu_);
        // something only I can do
        ...

        return;
    }
private:
    std::mutex mu_;
}
wakensky
  • 43
  • 1
  • 2
  • "When a lock_guard object is created, it attempts to take ownership of the mutex it is given. When control leaves the scope in which the lock_guard object was created, the lock_guard is destructed and the mutex is released." – BadZen Sep 22 '14 at 04:31
  • 1
    if you are unsure, you can always introduce new scope `{ }` and put lock_guard inside it. it also allow you to limit the scope of the lock_guard – Bryan Chen Sep 22 '14 at 05:09

0 Answers0