Boost logo

Boost :

From: Markus Schöpflin (markus.schoepflin_at_[hidden])
Date: 2002-05-10 08:24:33


Hi all,

the wait functions in boost::thread internally use a function
to_duration() which fails to give correct results under certain
circumstances (overflow of nsec to sec) with at least MSVC6SP5.

In my case this resulted in spurious 5 seconds waits instead of
50 milliseconds when using boost::condition::timed_wait().

The root of the problem is that you get quite some interesting
results when doing signed integer division on this compiler.
Try it with the following little program. :-)

--%<--
#include <iostream>
int main()
{
  unsigned long a = 2;
  signed long b = -2 / a;
  std::cout << b;
  return 0;
}
-->%--

As to_duration uses to following expression to calculate the
difference in milliseconds it is affected by this compiler bug
as well.

milliseconds = static_cast<unsigned>(
  ((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
    (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
-------^^^^^^^^^^^^^^^^^^ (sometimes negative)
    NANOSECONDS_PER_MILLISECOND));

The fix is easy enough. Change all the constants at the top of
"timeconv.inl" to macros and the compiler generates the right
code.

Markus


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk