]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Optimise std::future::wait_for
authorJonathan Wakely <jwakely@redhat.com>
Thu, 12 Nov 2020 21:25:14 +0000 (21:25 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 16 Nov 2020 21:13:52 +0000 (21:13 +0000)
commitc9f528dd1a14aacadec4638e7ee8ecff69fa0ee5
tree32d7ba3f7a21e8c8409b1556198edad55e74eebd
parentbfc7f358146acc8a001abe15f0061de64e0ebac5
libstdc++: Optimise std::future::wait_for

To poll a std::future to see if it's ready you have to call one of the
timed waiting functions. The most obvious way is wait_for(0s) but this
was previously very inefficient because it would turn the relative
timeout to an absolute one by calling system_clock::now(). When the
relative timeout is zero (or less) we're obviously going to get a time
that has already passed, but the overhead of obtaining the current time
can be dozens of microseconds.

This patch makes future::wait_for avoid waiting at all when the relative
timeout is zero or less, to avoid the unnecessary overhead of getting
the current time. This makes polling with wait_for(0s) take only a few
cycles instead of dozens of milliseconds.

libstdc++-v3/ChangeLog:

* include/std/future (future::wait_for): Do not wait for
durations less than or equal to zero.
* testsuite/30_threads/future/members/poll.cc: New test.

(cherry picked from commit 93fc47746815ea9dac413322fcade2931f757e7f)
libstdc++-v3/include/std/future
libstdc++-v3/testsuite/30_threads/future/members/poll.cc [new file with mode: 0644]