Re:In the C++ bag of tricks: scoped locks
by cjensen on Friday February 07, 2014 @03:15PM
(#7198442)
Solvable by creating a Preprocessor macro which creates a unique and complex-named identifier.
#define LOCKED(lk) for(int UNIQUE_ID=0; UNIQUE_ID<1 && !lk_lock(lk); lk_unlock(lk), UNIQUE_ID++)
Use __LINE__ to help make the id unique.
Re:In the C++ bag of tricks: scoped locks
by VikingCoder on Friday February 07, 2014 @04:24PM
(#7198839)
Then you still can't forget and do this:
lock(lk_var) lock(lk_var2)
{
lk_var = lk_var2;
}
Or this: lock(lk_var) { lock(lk_var2 { lk_var = lk_var2; } }
Using the pre-processor to solve a regular, every day, hum-drum, solved-a-billion-times RAII problem is bad news.
Re:In the C++ bag of tricks: scoped locks
by JoeAltmaier on Friday February 07, 2014 @02:05PM
(#7198063)
SUre you could. The{} make it work.
Re:In the C++ bag of tricks: scoped locks
by VikingCoder on Friday February 07, 2014 @04:22PM
(#7198821)
EDIT: I sit corrected. My post is wrong.
No, you can't, and no they don't.
lock(lk_var) {
lock(lk_var2) {
var = var2;
}
}
That expands to: for(int i=0; (i < 1) && !lk_lock(lk_var); lk_unlock(lk_var), i++)
{
for(int i=0; (i < 1) && !lk_lock(lk_var2); lk_unlock(lk_var2), i++)
{
var = var2;
}
}
You end up with an i scoped inside another i.
Re:In the C++ bag of tricks: scoped locks
by plorkyeran on Friday February 07, 2014 @04:58PM
(#7199037)
C++ has variable shadowing, so that works correctly.
for (int i = 0; i < 5; ++i) {
for (int i = 0; i < 5; ++i) {
printf("hello\n");
}
}
prints hello 25 times.
Re:In the C++ bag of tricks: scoped locks
by VikingCoder on Friday February 07, 2014 @05:04PM
(#7199070)
Oh right. My brain was stuck in another language. Wow, I forgot how much I'd forgotten about C++.
Re:In the C++ bag of tricks: scoped locks
by JoeAltmaier on Sunday February 09, 2014 @11:29PM
(#7209124)
Actually that's just C (name nesting)
Speaking of which, this means you can't have one of your locks scoped inside another one.