C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Forced stopping of RAII compliant execution threads. Do you have a working implementation?

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 14 Jul 2023 10:34:28 -0700
On Friday, 14 July 2023 09:51:40 PDT Yuri Petrenko via Std-Proposals wrote:
> No, I don't have any implementation yet.

I do. It's called Linux.

$ cat testthread.cpp
#include <thread>
#include <pthread.h>
#include <sys/poll.h>
#include <stdio.h>

struct S
{
    S() { puts("constructor"); }
    ~S() { puts("destructor"); }
};

int main()
{
    std::jthread thr([]() {
        S s;
        struct pollfd pfd = { .fd = 0, .events = POLLIN };
        while (poll(&pfd, 1, 100) >= 0)
            ; // simulate work;
    });
    poll(nullptr, 0, 1000); // sleep(1)
    pthread_cancel(thr.native_handle());
}
$ g++ -std=c++20 -g ./testthread.cpp
$ ./a.out
constructor
destructor


-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-07-14 17:34:30