2

I would like to use #define for locking/unlocking mutex. But on other platform where I only use single thread, I would like to disable locking and unlocking and do nothing.

In this case, how can I safely define it on apps that I don't use the feature?

For example,

#define SYS_LOCK sys_lock();
#define SYS_UNLOCK sys_unlock();

This is what I use in platforms that support multithreads.

But I want to disable it in on other platforms.

Is it safe to define like this so calling SYS_LOCK and SYS_UNLOCK does nothing?

#define SYS_LOCK
#define SYS_UNLOCK
Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • 1
    Yes, it is safe. But why you don't want to use functions instead? – HolyBlackCat Oct 27 '17 at 15:07
  • Yes. That should be perfectly fine. That's also how header files are usually flagged to avoid duplicate or recursive includes. – Mad Physicist Oct 27 '17 at 15:07
  • @HolyBlackCat. OP answered your question already. – Mad Physicist Oct 27 '17 at 15:08
  • 2
    semicolon at end of define is in wrong style. To your question I suggest `{}` – Jacek Cz Oct 27 '17 at 15:08
  • 1
    Possible duplicate of [Use #ifdefs and #define to optionally turn a function call into a comment](https://stackoverflow.com/questions/546997/use-ifdefs-and-define-to-optionally-turn-a-function-call-into-a-comment) – underscore_d Oct 27 '17 at 15:08
  • @JacekCz you mean like this? #define SYS_LOCK sys_lock() {} – Zack Lee Oct 27 '17 at 15:10
  • @HolyBlackCat calling functions can have unnecessary overheads right? – Zack Lee Oct 27 '17 at 15:11
  • The preprocessor does __text substitution__. Will your code be correct if you substitute `SYS_(UN)LOCK` for nothing in your code? If yes, you're good to go. – ForceBru Oct 27 '17 at 15:12
  • @ZackLee You could make them inline. IMO, a macro should be used as a last resort. – HolyBlackCat Oct 27 '17 at 15:14
  • @MadPhysicist Maybe I need more coffee, but I'm not seeing where they did that. :/ – HolyBlackCat Oct 27 '17 at 15:14
  • 1
    You'd be better off defining them as `#define SYS_LOCK() sys_lock()` and `#define SYS_LOCK() `. It will make their use in code more natural looking. – StoryTeller - Unslander Monica Oct 27 '17 at 15:28
  • @HolyBlackCat when someone tells that your question was already answered but you don't see any answers to it then it is usually a symptom that they understood your question differently than what you thought that you asked. Better post answer where you elaborate your idea of how to do it without using preprocessor. – Öö Tiib Oct 27 '17 at 15:30

1 Answers1

3

#define simply declares that this name exists to the pre-processor. Not only is the concept of empty #define harmless, it is even widely used just because you can test whether the macro has been defined without caring for its meaning.

It is mostly used to enable or disable some functionalities in imported files. Simple example from the pylon API (I am coincidentally working with):

#   if defined(_WIN32) && !defined(_WIN64)
#       define PYLON_32_BUILD
#   elif defined(_WIN32) && defined(_WIN64)
#       define PYLON_64_BUILD
#   else
#       error unsupported wordsize (32/64 platform)
#   endif

This statement will decide whether to include the 32-bit or the 64-bit version of the API

Adalcar
  • 1,458
  • 11
  • 26