Consider the following piece of code
#include <chrono>
#include <iostream>
#include <thread>
int main()
{
using std::chrono::system_clock;
using std::chrono::milliseconds;
using std::chrono::nanoseconds;
using std::chrono::duration_cast;
const auto duration = milliseconds(100);
const auto start = system_clock::now();
std::this_thread::sleep_for(duration);
const auto stop = system_clock::now();
const auto d_correct = duration_cast<nanoseconds>(duration).count();
const auto d_actual = duration_cast<nanoseconds>(stop - start).count();
std::cout << "Difference is " << d_actual << ", and it should be roughly " << d_correct << "\n";
}
What we expect is something on the line of
Difference is 100039989, and it should be roughly 100000000
See this demo where it works absolutely fine.
However, on my machine, there are several compilers installed which seem to cause a malconfiguration according to this answer here on Stack Overflow.
Hence I tried the suggested fix: Setting the correct LD_LIBRARY_PATH
.
These are the combinations with output I tried (among others with 4.4 and 4.6...)
g++-4.7 time.cpp -pthread -std=c++11; LD_LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.7/ ./a.out
Difference is 100126, and it should be roughly 100000000
g++-4.7 time.cpp -pthread -std=c++11; LD_LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.8/ ./a.out
Difference is 100132, and it should be roughly 100000000
g++-4.8 time.cpp -pthread -std=c++11; LD_LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.7/ ./a.out
Difference is 100085953, and it should be roughly 100000000
g++-4.8 time.cpp -pthread -std=c++11; LD_LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.8/ ./a.out
Difference is 100156418, and it should be roughly 100000000
It seems that no matter how, compiling with g++-4.8
works fine using any of the libstdc++
, while compiling with g++-4.7
results in a broken situation.
Am I doing anything wrong here in the compiler / binary invocation or is it a bug in g++-4.7
? (It's g++-4.7.3
and g++-4.8.1
to be specific)
For (the probably most ugly) workaround, I can of course measure for a tiny amount of time, compare it against the expected difference and come up with a factor. However I would very much like to solve this elegantly.
apt-get
stuff. So there is a bug somewhere & I want to know where. – Swagertyoperator-
is broken). – Madlynmadman