https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94996

            Bug ID: 94996
           Summary: jthread should stop and join the associated thread
                    before being assigned.
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hotwatermorning at gmail dot com
  Target Milestone: ---

Hi,

A joinable std::jthread should stop and join the associated thread before being
assigned, but it seems that current implementation doesn't.

c.f. https://eel.is/c++draft/thread.jthread.class#thread.jthread.cons-13
c.f. https://github.com/josuttis/jthread/pull/35

Step to reproduce: Build and run this code snippet with gcc-10.1 with
`-std=c++20` option.

#include <cassert>
#include <iostream>
#include <thread>
#include <chrono>

int main()
{
    std::cout << "Start a thread." << std::endl;
    std::jthread th([](std::stop_token st) {
        for( ; st.stop_requested() == false; ) {
            std::cout << "." << std::flush;
            std::this_thread::sleep_for(std::chrono::milliseconds(300));
        }
    });

    std::stop_token st = th.get_stop_token();

    std::this_thread::sleep_for(std::chrono::milliseconds(3000));

    th = std::jthread();

    // `th` should stop and join the associated thread before being assigned.
    assert(st.stop_requested());

    std::cout << "Finished." << std::endl;
}

Reply via email to