Date: 2010may3
Language: C/C++
Keywords: no-op
Q. How can I do an assembler-like NOOP in C/C++ ?
A. If you really want to do nothing you can do:
#define NOOP /* do nothing */
ExampleUse()
{
NOOP
}
or
// Notice there is no trailing semicolon
#define NOOP_FUNCTION do { /* do nothing */ } while (false)
ExampleUse()
{
NOOP_FUNCTION();
// This might not get compiled down to nothing
}
or
inline void Noop()
{
/* do nothing */
}
ExampleUse()
{
Noop();
}
or
In some environments, in <stdarg.h> you have:
#define va_end(ap) ((void)0)
which does very little.
ExampleUse()
{
va_end(ap);
}
But sometimes you want a very small time delay. So you can try:
true;
ExampleUse()
{
true; // Very small delay
}
In Windows you can do:
Sleep(0);
Which appears to do very little. It will sleep zero milliseconds
and but also give control to another process if it is waiting.