Skip to content

Destructor sometimes hangs #1

@pfeatherstone

Description

@pfeatherstone

The destructor of task_system can sometimes hang. So i believe there is a race somewhere. I'm using the following code where "Done" is sometimes not printed.

{
    ts::task_system tp;

    const size_t ntests = 1000000;
    std::atomic<size_t>     counter = 0;
    std::mutex              lock;
    std::condition_variable event;
    bool                    done{false};

    const auto start = high_resolution_clock::now();

    for (size_t i = 0 ; i < ntests ; ++i)
    {
        tp.schedule([&] {
            ++counter;

            if (counter == ntests)
            {
                {
                    std::unique_lock l{lock};
                    done = true;
                }
                event.notify_one();
            }
        });
    }

    printf("Enqueued all tasks...\n");
    {
        std::unique_lock l{lock};
        event.wait(l, [&]{return done;});
    }
    const auto stop = high_resolution_clock::now();
    const auto delta = duration_cast<milliseconds>(stop-start).count() * 1e-3;
    printf("Enqueued all tasks... Done in %f seconds\n", delta);
}
printf("Done\n");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions