0

I'm calling the following from the main() function, and also in other created threads. While, it works fine in the created threads, it returns a large junk value in my main().

long getTimeMilliSecs()
{
    struct timeval tempTime;
    gettimeofday(&tempTime, NULL);
    long retVal = 1000000*tempTime.tv_sec + tempTime.tv_usec;
    return retVal;
}

Using Linux Ubuntu with gcc. Does anyone know the reason for this peculiar behaviour? Thank You!

Using it in this fashion:

main()
{
:
   emuTime = getTimeMilliSecs();
}

Thread side code:

long time1 = getTimeMilliSecs();
printf("time1: %ld  emuTime: %ld\n", time1, emuTime);   


OUTPUT:
time1: 9006806 emuTime: 1380680868658357

Similar way of calling in threads.


From comments: Updated! emuTime is also of type long.

alk
  • 69,737
  • 10
  • 105
  • 255
harshalizee
  • 129
  • 1
  • 3
  • 12
  • what does it return? What are you expecting it to return? – John3136 Oct 02 '13 at 02:12
  • Did you check the return value? – Charlie Burns Oct 02 '13 at 02:15
  • Can you show code that replicates the problem (not just your function, but how it was called in `main()` and other threads - and how you look at it. Might it be that the problem is in your `printf` formatting - are you using `printf("value %d", getTimeMilliSecs);` or `printf("value %ld",getTimeMilliSecs);` since this returns a `long`...? – Floris Oct 02 '13 at 02:21
  • By the way - as written, your function returns microseconds, although you call it `milliSecs`. Does that bother you? – Floris Oct 02 '13 at 02:22
  • I'm printing it as %ld on both sides. Yes, it says Milli, bcoz I'm dividing by 1000.0 wherever neccessary only. long emuTime; emuTime = getTimeMilliSecs(); – harshalizee Oct 02 '13 at 02:28
  • On the main thread it returns a values like 1380680868658357 (16-digit nos) while on the thread it returns properly calculated values – harshalizee Oct 02 '13 at 02:32
  • Change your `long` to `long long` and `1000000` to `1000000LL`. – chux - Reinstate Monica Oct 02 '13 at 02:34
  • 1
    It *should* return a value like `1380680868658357`, which would be shortly after Wed 2013-10-02 02:27:48 UTC, just a few minutes ago as I type this. The question is why it's returning something else from another thread. Show us the actual value it's returning. The value returned by your function is microseconds since 1970. (How big is `long int` on your system?) – Keith Thompson Oct 02 '13 at 02:34
  • 2
    Why not call it getTimeMicroSecs()? – chux - Reinstate Monica Oct 02 '13 at 02:35
  • Showing code that assigns the result to a variable is not helpful. Show us code that actually prints the value, and tell us exactly what it prints. – Keith Thompson Oct 02 '13 at 02:37
  • Updated! emuTime is also of type long. – harshalizee Oct 02 '13 at 02:47
  • 1
    Now why do you think that `9006806` is a "properly calculated value"? – Keith Thompson Oct 02 '13 at 04:01
  • According to [this](http://stackoverflow.com/q/3220224/827263), `gettimeofday` is thread-safe. Can you show us a *small* self-contained program that exhibits the problem? See http://sscce.org/ – Keith Thompson Oct 02 '13 at 04:13
  • It works for me: https://gist.github.com/Keith-S-Thompson/6789119 – Keith Thompson Oct 02 '13 at 04:22
  • Are you *sure* that it's the same `emuTime` in both? It could be that your thread has its own copy of `emuTime` that is never set by `main()`. Try printing `emuTime` inside main, and also try to set it to `0` instead of the time and make sure the threads also print `0`. – Adam Oct 02 '13 at 05:18
  • I tried it too, in a seperate program. Couldn't reproduce the same problem. I'll look through it, with a fine comb, and report back. Thank you, all, for the help! Much appreciate it! Especially Keith – harshalizee Oct 02 '13 at 06:29
  • First, `main()` is getting the correct answer. The threads are mis-behaving. My money is that your thread code is not using the correct prototype, if any, for `getTimeMilliSecs()` - maybe assuming a 32-bit response from `getTimeMilliSecs()`. What prototype (declaration) do the threads see? – chux - Reinstate Monica Oct 02 '13 at 12:31

0 Answers0