Possible to define a function-like macro with a variable body?

后端 未结 4 1790
小鲜肉
小鲜肉 2020-12-18 00:40

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

4条回答
  •  再見小時候
    2020-12-18 00:42

    Here's a start, but you may need to tweak it:

    #define synchronized(lock, func, args...) do { \
        pthread_mutex_lock(&(lock)); \
        func(##args); \
        pthread_mutex_unlock(&(lock)); \
    } while (0)
    

    Use like this (unfortunately, not the Java-like syntax you wanted):

    synchronized(x, do_thing, arg1, arg2);
    

提交回复
热议问题