Earlier quoted context omitted.
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.
In the C++ bag of tricks: scoped locks
11–20 of 56 posts
Re: In the C++ bag of tricks: scoped locks
#12The 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…
As a general rule, if your C++ compiler can statically see the layout of your structures and the implementation of your functions, it will produce code as efficient as inlining everything yourself. Sufficiently complex functions make the benefits of inlining a judgment call on the compiler's part, but for something as simple as an RAII scoped lock, the abstraction is free.
Re: In the C++ bag of tricks: scoped locks
#13C++11 has scoped locks built-in, doesn't it?
Re: In the C++ bag of tricks: scoped locks
#14Re: In the C++ bag of tricks: scoped locks
#15You can do some of this with the cleanup attribute in GCC, although it doesn't trigger in the case of a longjmp.
Looks like their project already uses C++ though, so why don't they just use C++ for this is beyond me.
Re: In the C++ bag of tricks: scoped locks
#16You can do some of this with the cleanup attribute in GCC, although it doesn't trigger in the case of a longjmp.
if you can rely on gcc, cleanup attribute is probably the best you can do. The solution in original post doesn't cope with "break", "continue", "return" (and of course, "goto", but I don't remember if cleanup attribute handles that either). Looks like their project already uses C++ though, so why don't they just use C++ for this is beyond me.
Re: In the C++ bag of tricks: scoped locks
#17Re: In the C++ bag of tricks: scoped locks
#18You can do some of this with the cleanup attribute in GCC, although it doesn't trigger in the case of a longjmp.
if you can rely on gcc, cleanup attribute is probably the best you can do. The solution in original post doesn't cope with "break", "continue", "return" (and of course, "goto", but I don't remember if cleanup attribute handles that either). Looks like their project already uses C++ though, so why don't they just use C++ for this is beyond me.
Re: In the C++ bag of tricks: scoped locks
#19Thank you vinkelhake for nailing the the real problem (exceptions) and bothering to check whether the stack overhead really exists.
Just wanted to chime in with a comment because HN doesn't show vote counts, so future readers will "see" my upvote on vinkelhake's comment.
Re: In the C++ bag of tricks: scoped locks
#20Speaking of which, this means you can't have one of your locks scoped inside another one.