In the C++ bag of tricks: scoped locks
blog.skanev.org
In the C++ bag of tricks: scoped locks
1–10 of 56 posts
Re: In the C++ bag of tricks: scoped locks
#2 #define LOCKED(lk) for(int i=lk_lock(lk); i == 0; lk_unlock(lk), i++)
Probably better to test for 0 exactly, in case it fails. You don't want it to unlock twice just because it returns -1.Re: In the C++ bag of tricks: scoped locks
#3Re: In the C++ bag of tricks: scoped locks
#4Re: In the C++ bag of tricks: scoped locks
#5> 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
#6The 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…
- 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
#7The 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…
Re: In the C++ bag of tricks: scoped locks
#8Re: In the C++ bag of tricks: scoped locks
#9The 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
#10The 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.