boostorg / thread

Boost.org thread module
http://boost.org/libs/thread
198 stars 162 forks source link

Fix Issue 271 #272

Closed austin-beer closed 5 years ago

austin-beer commented 5 years ago
austin-beer commented 5 years ago

This is the program I used to test this code. It's a modified version of the test code used in Issue 271.

#include <iostream>
#include "boost/chrono/system_clocks.hpp"
#include "boost/thread/executors/scheduler.hpp"

int main()
{
    boost::executors::scheduler<boost::chrono::steady_clock> sc;
    auto start = boost::chrono::steady_clock::now();
    int count = 10;
    for (int i = 0; i < count; ++i)
    {
        boost::chrono::seconds delay(count - i);
        auto time = boost::chrono::duration_cast<boost::chrono::milliseconds>(boost::chrono::steady_clock::now() - start);
        std::cout << time << ": Run " << static_cast<char>('A' + i) << " in " << delay << std::endl;
        sc.submit_after([=] {
            auto time = boost::chrono::duration_cast<boost::chrono::milliseconds>(boost::chrono::steady_clock::now() - start);
            std::cout << time << ": Ran " << static_cast<char>('A' + i) << " after " << delay << std::endl;
        }, delay);
        boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
    }
    boost::this_thread::sleep_for(boost::chrono::seconds(count + 2));
    return 0;
}

And here's the output with this fix applied:

0 milliseconds: Run A in 10 seconds
500 milliseconds: Run B in 9 seconds
1000 milliseconds: Run C in 8 seconds
1500 milliseconds: Run D in 7 seconds
2000 milliseconds: Run E in 6 seconds
2500 milliseconds: Run F in 5 seconds
3000 milliseconds: Run G in 4 seconds
3500 milliseconds: Run H in 3 seconds
4000 milliseconds: Run I in 2 seconds
4500 milliseconds: Run J in 1 second
5500 milliseconds: Ran J after 1 second
6000 milliseconds: Ran I after 2 seconds
6500 milliseconds: Ran H after 3 seconds
7000 milliseconds: Ran G after 4 seconds
7500 milliseconds: Ran F after 5 seconds
8000 milliseconds: Ran E after 6 seconds
8500 milliseconds: Ran D after 7 seconds
9000 milliseconds: Ran C after 8 seconds
9500 milliseconds: Ran B after 9 seconds
10000 milliseconds: Ran A after 10 seconds