I've been looking at the GCC docs for defining macros and it looks like what I want isn't possible, but I figure if it is, someone here would know.
What I want to do is define this macro:
synchronized(x) {
do_thing();
}
Which expands to:
{
pthread_mutex_lock(&x);
do_thing();
pthread_mutex_unlock(&x);
}
In C++ I could just make a SynchronizedBlock
object that gets the lock in its constructor and unlocks in the destructor, but I have no idea how to do it in C.
I realize I could use a function pointer in the form synchronized(x, &myfunction);
, but my goal is to make some C code look as much like Java as possible. And yes, I know this is evil.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…