[Libwebsockets] [libwebsockets] #76: Wrong timing in test-server.c

Trac trac at libwebsockets.org
Tue Jul 1 08:39:12 CEST 2014

#76: Wrong timing in test-server.c
 Reporter:  tiko                    |      Owner:
     Type:  defect                  |     Status:  new
 Priority:  major                   |  Milestone:
Component:  libwebsocket test apps  |    Version:  1.0
 Keywords:  timing, polling         |
 we are using your " test-server.c" as a template and noticed a strange
 double call of the callback functions.
 If you look close at line 902-915 you find the minor bug:
 You use the function "gettimeofday()" which holds the seconds and
 Unfortunately you use the microseconds to check, if you run out of time.
 The member "tv_usec" only counts till 1000000, which is exactly one
 second, and then starts over.
 this leads to the fact, that (tv.tv_usec - oldus) becomes negative, and
 therefore very big because of the uint cast.

 So you do two callbacks.
 - One, if your difference becomes naturally greater then the given value.
 - Second, if tv.tv_usec starts over.

 We are using chrono like this :
     while (n >= 0 && !force_exit) {

         std::chrono::high_resolution_clock::time_point t2 =
         std::chrono::duration<float> time_span =
 std::chrono::duration_cast<std::chrono::duration<float>>(t2 - t1);

         // Check if the time difference is bigger than the value in
 seconds, then execute the callbacks
         if (time_span.count() > 0.1) {
             if(frameReceived == true){
                           frameReceived = false;
             t1 = std::chrono::high_resolution_clock::now();


Ticket URL: <http://libwebsockets.org/trac/libwebsockets/ticket/76>
libwebsockets <http://libwebsockets.org>
libwebsockets C library

More information about the Libwebsockets mailing list