Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like bdonian says, if you're only sending a few hundred messages per second, <code>gettimeofday</code> is going to be fast enough.</p> <p>However, if you were sending millions of messages per second, it might be different (but you should still <strong>measure</strong> that it is a bottleneck). In that case, you might want to consider something like this:</p> <ul> <li>have a global variable, giving the current timestamp in your desired accuracy</li> <li>have a dedicated background thread that does nothing except update the timestamp (if timestamp should be updated every T units of time, then have the thread sleep some fraction of T and then update the timestamp; use real-time features if you need to)</li> <li>all other threads (or the main process, if you don't use threads otherwise) just reads the global variable</li> </ul> <p>The C language does not guarantee that you can read the timestamp value if it is larger than <code>sig_atomic_t</code>. You could use locking to deal with that, but locking is heavy. Instead, you could use a <code>volatile sig_atomic_t</code> typed variable to index an array of timestamps: the background thread updates the next element in the array, and then updates the index. The other threads read the index, and then read the array: they might get a tiny bit out-of-date timestamp (but they get the right one next time), but they do not run into the problem where they read the timestamp at the same time it is being updated, and get some bytes of the old value and some of the new value.</p> <p>But all this is much overkill for just hundreds of messages per second.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload