Live data from Hacker News

In the C++ bag of tricks: scoped locks

blog.skanev.org

1–10 of 56 posts

Re: In the C++ bag of tricks: scoped locks

#5
The real advantage of a scoped lock is that it promises to release the mutex, no matter how the scope is exited. If this LOCKED macro is used and an exception is thrown from within the block, that mutex now stays locked.

> The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition.

Not so fast. A decent compiler will eliminate the overhead and not actually allocate stack space for the lock. Here's an example using one of the lock guards in C++:

    struct foo {
      int var;
      std::mutex m;
    };
    
    void process1(foo& f) {
      std::lock_guard lock(f.m);
      ++f.var;  
    }

    void process2(foo& f) {
      f.m.lock();
      ++f.var;  
      f.m.unlock();
    }
GCC 4.8.1 generates identical code for process1 and process2.

Re: In the C++ bag of tricks: scoped locks

#6

The real advantage of a scoped lock is that it promises to release the mutex, no matter how the scope is exited. If this LOCKED macro is used and an exception is thrown from within the block, that mutex now stays locked. > The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition. Not so fast. A decent compiler…

This. For at least three typical 'best practice' reasons. I know there are times best practices can be nicely ignored, but this is not one of those occasions. On the contrary. Let me lay them out again:

- these are the days of C++11 where scoped locks are readily available

- premature optimization

- macros are evil

Re: In the C++ bag of tricks: scoped locks

#7

The real advantage of a scoped lock is that it promises to release the mutex, no matter how the scope is exited. If this LOCKED macro is used and an exception is thrown from within the block, that mutex now stays locked. > The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition. Not so fast. A decent compiler…

Wait... if it's identical, how can it be safe against exceptions? `process2` isn't.

Re: In the C++ bag of tricks: scoped locks

#9
post #6

The real advantage of a scoped lock is that it promises to release the mutex, no matter how the scope is exited. If this LOCKED macro is used and an exception is thrown from within the block, that mutex now stays locked. > The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition. Not so fast. A decent compiler…

This. For at least three typical 'best practice' reasons. I know there are times best practices can be nicely ignored, but this is not one of those occasions. On the contrary. Let me lay them out again: - these are the days of C++11 where scoped locks are readily available - premature optimization - macros are evil

One of the reasons C++ gets bad reputation, is those Cisms that people insist on writing, when the language offers much better alternatives.

Re: In the C++ bag of tricks: scoped locks

#10

The real advantage of a scoped lock is that it promises to release the mutex, no matter how the scope is exited. If this LOCKED macro is used and an exception is thrown from within the block, that mutex now stays locked. > The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition. Not so fast. A decent compiler…

Wait... if it's identical, how can it be safe against exceptions? `process2` isn't.

gcc can statically determine that incrementing f.var will not throw.
Post reply on HN