ISO/IEC JTC1 SC22 WG21 N2539 = 08-0049 - 2008-02-25
Paul E. McKenney, paulmck@linux.vnet.ibm.com
Michael Wong, michaelw@ca.ibm.com
Consideration of this proposal is deferred until TR2.
This document presents a new interface for time durations as proposed by the POSIX C++ Binding Working Group at the 2008 Bellevue meeting. This document is based on the latest Working Draft (n2521), Why duration Should Be a Type in C++0x (n2526), and Custom Time Duration Support (n2498). It accepts the findings in n2526 for a need to minimize the number of distinct interfaces for time duration, and represents a follow-up on the alternatives as proposed in n2526.
See Why duration Should Be a Type in C++0x (n2526).
See See Why duration Should Be a Type in C++0x (n2526).
We propose maintaining a single implementation-defined type that holds
time durations, with member functions that permit conversions from that
implementation-defined type to and from nanoseconds, microseconds,
milliseconds, seconds, minutes, and hours.
We strongly suggest that implementations use the same units for
duration
as are used for system_time
.
We further propose that this type occupy the std
namespace
rather then a part of std::thread
namespace, as we see this
as a generally useful type rather then one that is specifically useful
to thread.
namespace std {
class duration
{
public:
// traits information
typedef implementation-defined tick_type;
static const tick_type ticks_per_second = implementation-defined;
static const tick_type seconds_per_tick = implementation-defined;
static const bool is_subsecond = implementation-defined;
typedef enum duration_unit {
duration_native,
duration_nanoseconds, duration_microseconds,
duration_milliseconds, duration_seconds,
duration_minutes, duration_hours
} duration_unit;
// construct/copy/destroy
duration(duration_unit u, long long t = 0);
duration(duration_unit u, double t = 0.0);
duration(long long tps, long long stp, long long t = 0);
duration(long long tps, long long stp, double t = 0.0);
// modifiers
duration& operator+=(const duration &d);
duration& operator-=(const duration &d);
duration& operator*=(long multiplier);
duration& operator*=(double multiplier);
duration& operator/=(long divisor);
duration& operator/=(double divisor);
// observers
tick_type count() const;
long long nanoseconds() const;
double d_nanoseconds() const;
long long microseconds() const;
double d_microseconds() const;
long long milliseconds() const;
double d_milliseconds() const;
long long seconds() const;
double d_seconds() const;
long long minutes() const;
double d_minutes() const;
long long hours() const;
double d_hours() const;
long long user_defined(long long tps, long long stp) const;
double d_user_defined(long long tps, long long stp) const;
// operations
tick_type operator-() const;
};
}
tick_type
Implementation-defined type that defines the underlying timer tick.
ticks_per_second
The number of ticks per second or zero when the number of ticks per second is less than one and the number of seconds per tick is integral.
seconds_per_tick
The number of seconds per tick or zero when the number of seconds per tick is less than one and the number of ticks per second is integral.
seconds_per_tick
The number of seconds per tick or zero when the number of seconds per tick is less than one and the number of ticks per second is integral.
is_subsecond
This is equal to 1 when
seconds_per_tick==0
.
duration_unit
Allows the user to specify a limited number of predefined time units, including nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and an implementation-defined native time unit.
duration
(constructors)
Allows constructing a native-time value from either a standard (nanoseconds through hours) or user-defined time value. The user can specify either an integral or a floating-point value. The
tps
andstp
arguments are analogous to theticks_per_second
andseconds_per_tick
members.
operator +=
Increments the specified
duration
object by theduration
object passed in as the argument. Theduration
constructors may be used to add time durations in any desired units.
operator -=
Decrements the specified
duration
object by theduration
object passed in as the argument. Theduration
constructors may be used to subtract time durations in any desired units.
operator *=
Multiplies the specified
duration
object by the specified integral or floating-point argument.
operator /=
Divides the specified
duration
object by the specified integral or floating-point argument.
count
Returns the implementation-defined number of ticks.
nanoseconds, d_nanoseconds
Returns the
duration
in nanoseconds.
microseconds, d_microseconds
Returns the
duration
in microseconds.
milliseconds, d_milliseconds
Returns the
duration
in milliseconds.
seconds, d_seconds
Returns the
duration
in seconds.
minutes, d_minutes
Returns the
duration
in minutes.
hours, d_hours
Returns the
duration
in hours.
user_defined, d_user_defined
Returns the
duration
in user-defined units.
This proposal differs from the language in n2521 in the following respects:
duration
class, but division by the constant
1,000 should suffice given that the ratio of microseconds to
milliseconds is unlikely to change in the foreseeable future.
This latter approach also has the advantage of avoiding roundoff
errors due to coarse-grained implementations.
ticks_per_second==3
and
seconds_per_tick==2
would result in three ticks
every two seconds.
tps
and
stp
constructor arguments.
3.5 * std::threads::minute
.
If the value of std::threads::minute
was not integral,
large multipliers could result in arbitrarily large errors.
//N2378 syntax
duration operator"ms"(double ms){ . . . }
duration frames_per_sec=200ms;
//This paper"s syntax
duration frames_per_sec(duration_milliseconds, 200);
This approach could be attractive if it is possible to create a "hub-and-spoke" conversion regimen, so that the number of conversion functions is O(N) rather than O(N2). The issues surrounding this are explored in the following appendix.
tick_type
.
This was felt to be out of scope for this proposal, but
should be considered for TR2.
There are at least two ways to apply user-defined literals to specify durations:
duration
type.
This places the conversions into the user-defined literal
operators.
One issue with both of these approaches is that user-defined literals have a global namespace.