std::chrono behaves different in arm

546 Views Asked by At

So the following code I am using for a hacky production fix due to time constraints. Basically I have a static function that is called from many places, much more than intended and it is causing another section of the application to choke. So I thought I would come up with a quick fix to limit the calls to the overworked function to once every two seconds. This works just fine in x86 using clang or gcc.

#include <chrono>
#include <iostream>
#include <unistd.h>
#include <thread>

static void staticfunction()
{
    static std::mutex mutex;
    static auto t0(std::chrono::high_resolution_clock::now());
    std::unique_lock<std::mutex> lg_mutex(mutex, std::try_to_lock);
    if( lg_mutex.owns_lock())
    {
        auto t1 = std::chrono::high_resolution_clock::now();
        if( 2000 <= std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count() )
        {
            // Make a check in other section of application
            std::cout << "Check true after " << std::dec
                      << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()
                      << " ms.\n";
            t0 = std::chrono::high_resolution_clock::now();
        }
    }
}
int main()
{
    while(true) {
    std::thread t1(staticfunction);
    std::thread t2(staticfunction);
    std::thread t3(staticfunction);
    std::thread t4(staticfunction);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
}
return 0;

Prints Check true after 2000 ms. Check true after 2000 ms. Check true after 2002 ms. Check true after 2005 ms. ....

However, for our ARM controller I cross compiled using Linaro 7.1 and now the condition for the if stmt isn't satisfied until 10 seconds has passed. I was curious and compared against 1 second instead of two (duration_cast of ms vs seconds doesn't change anything) and if(1 <= ....count()) was true after half a second.

Is this a bug in the Linaro compiler? Or are clocks for our ARM controller off? Cross compile flags are -mcpu=cortex-a7 -mfloat-abi=hard -marm -march=armv7ve if that makes a difference

EDIT: multithreaded, same output.

0

There are 0 best solutions below