Is this execution reordering possible

  as-if, c++, instruction-reordering

As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code:

C = A + B;
D = E + F;

The compiler or the CPU may execute D = E + F before C = A + B. I can understand that.

Today, my colleague tried to build a log library with C++. His idea is to use constructor & destructor to make some logs with the help of some overloading stream functions, such as operator<<().

Basically, he offered such a kind of class:

class Log
{
public:
    Log() { streamObj << "start: " << getCurrentTime() << endl; }
    ~Log() { streamObj << "end: " << getCurrentTime() << endl; }
};

Now, I’m the user of the log library. My colleague told me that I could use the library as below:

void func()
{
    Log log;
    // do something1
    // do something2
    // do something3
    return;
}

So when the first line is executed, the constructor is invoked so I can get a "start" in the log. And when the function returns, the destructor of log will be invoked so I will get an end in the log. With the help of the object log, we can clearly find the start of the function and the end of the function.

That sounds clear and great.

However, as I mentioned at the beginning of the post, the machine may do some reordering as it wants. So I’m now wondering if it is possible that the constructor of log is invoked later than we think and the destructor of the log is invoked sooner than we think so that the log can’t work as we expected. I mean, the code of func did look as above but when it was compiled or was executed, the real order became:

// do something1
Log log;
// do something2
// call the destructor of `log`
// do something3
return

BTW, the stream in the class Log is direct into somewhere else, such as a file, or a shared memory or a TCP socket.

So am I reasonable? Or this kind of reordering will never happen? If it may happen, is there some technique to forbid this kind of reordering or some technique to offer an useable log library which can tell us the start and the end of any function?

Source: Windows Questions C++

LEAVE A COMMENT