I call h.destroy() in final_suspend to destroy the coroutine automatically when it finishes execution and then I resume awaiting coroutine (that awaits the task to complete). I found a question about this technique and an answer explaining why it should work.
As far as I can see, this technique really works, but not with MSVC 2022 that calls task destructor twice, see the code below:
#include <coroutine>
#include <optional>
#include <iostream>
#include <thread>
#include <chrono>
#include <queue>
#include <vector>
// simple timers
// stored timer tasks
struct timer_task
{
std::chrono::steady_clock::time_point target_time;
std::coroutine_handle<> handle;
};
// comparator
struct timer_task_before_cmp
{
bool operator()(const timer_task& left, const timer_task& right) const
{
return left.target_time > right.target_time;
}
};
std::priority_queue<timer_task, std::vector<timer_task>, timer_task_before_cmp> timers;
inline void submit_timer_task(std::coroutine_handle<> handle, std::chrono::nanoseconds timeout)
{
timers.push(timer_task{ std::chrono::steady_clock::now() + timeout, handle });
}
//template <bool owning>
struct UpdatePromise;
//template <bool owning>
struct UpdateTask
{
// declare promise type
using promise_type = UpdatePromise;
UpdateTask(std::coroutine_handle<promise_type> handle) :
handle(handle)
{
std::cout << "UpdateTask constructor." << std::endl;
}
UpdateTask(const UpdateTask&) = delete;
UpdateTask(UpdateTask&& other) : handle(other.handle)
{
std::cout << "UpdateTask move constructor." << std::endl;
}
UpdateTask& operator = (const UpdateTask&) = delete;
UpdateTask& operator = (const UpdateTask&& other)
{
handle = other.handle;
std::cout << "UpdateTask move assignment." << std::endl;
return *this;
}
~UpdateTask()
{
std::cout << "UpdateTask destructor." << std::endl;
}
std::coroutine_handle<promise_type> handle;
};
struct UpdatePromise
{
std::coroutine_handle<> awaiting_coroutine;
UpdateTask get_return_object();
std::suspend_never initial_suspend()
{
return {};
}
void unhandled_exception()
{
std::terminate();
}
auto final_suspend() noexcept
{
// if there is a coroutine that is awaiting on this coroutine resume it
struct transfer_awaitable
{
std::coroutine_handle<> awaiting_coroutine;
// always stop at final suspend
bool await_ready() noexcept
{
return false;
}
std::coroutine_handle<> await_suspend(std::coroutine_handle<UpdatePromise> h) noexcept
{
// resume awaiting coroutine or if there is no coroutine to resume return special coroutine that do
// nothing
std::coroutine_handle<> val = awaiting_coroutine ? awaiting_coroutine : std::noop_coroutine();
h.destroy();
return val;
}
void await_resume() noexcept {}
};
return transfer_awaitable{ awaiting_coroutine };
}
void return_void() {}
// use `co_await std::chrono::seconds{n}` to wait specified amount of time
auto await_transform(std::chrono::milliseconds d)
{
struct timer_awaitable
{
std::chrono::milliseconds m_d;
// always suspend
bool await_ready()
{
return m_d <= std::chrono::milliseconds(0);
}
// h is a handler for current coroutine which is suspended
void await_suspend(std::coroutine_handle<> h)
{
// submit suspended coroutine to be resumed after timeout
submit_timer_task(h, m_d);
}
void await_resume() {}
};
return timer_awaitable{ d };
}
// also we can await other UpdateTask<T>
auto await_transform(UpdateTask& update_task)
{
if (!update_task.handle)
{
throw std::runtime_error("coroutine without promise awaited");
}
if (update_task.handle.promise().awaiting_coroutine)
{
throw std::runtime_error("coroutine already awaited");
}
struct task_awaitable
{
std::coroutine_handle<UpdatePromise> handle;
// check if this UpdateTask already has value computed
bool await_ready()
{
return handle.done();
}
// h - is a handle to coroutine that calls co_await
// store coroutine handle to be resumed after computing UpdateTask value
void await_suspend(std::coroutine_handle<> h)
{
handle.promise().awaiting_coroutine = h;
}
// when ready return value to a consumer
auto await_resume()
{
}
};
return task_awaitable{ update_task.handle };
}
};
inline UpdateTask UpdatePromise::get_return_object()
{
return { std::coroutine_handle<UpdatePromise>::from_promise(*this) };
}
// timer loop
void loop()
{
while (!timers.empty())
{
auto& timer = timers.top();
// if it is time to run a coroutine
if (timer.target_time < std::chrono::steady_clock::now())
{
auto handle = timer.handle;
timers.pop();
handle.resume();
}
else
{
std::this_thread::sleep_until(timer.target_time);
}
}
}
// example
using namespace std::chrono_literals;
UpdateTask TestTimerAwait()
{
using namespace std::chrono_literals;
std::cout << "testTimerAwait started." << std::endl;
co_await 1s;
std::cout << "testTimerAwait finished." << std::endl;
}
UpdateTask TestNestedTimerAwait()
{
using namespace std::chrono_literals;
std::cout << "testNestedTimerAwait started." << std::endl;
auto task = TestTimerAwait();
co_await 2s;
//co_await task;
std::cout << "testNestedTimerAwait finished." << std::endl;
}
// main can't be a coroutine and usually need some sort of looper (io_service or timer loop in this example)
int main()
{
auto task = TestNestedTimerAwait();
// execute deferred coroutines
loop();
}
the output with MSVC 2022 is:
UpdateTask constructor.
testNestedTimerAwait started.
UpdateTask constructor.
testTimerAwait started.
testTimerAwait finished.
testNestedTimerAwait finished.
UpdateTask destructor.
UpdateTask destructor.
UpdateTask destructor.
but the output with GCC 11.1.0 is:
UpdateTask constructor.
testNestedTimerAwait started.
UpdateTask constructor.
testTimerAwait started.
testTimerAwait finished.
testNestedTimerAwait finished.
UpdateTask destructor.
UpdateTask destructor.
as you can see there is one extra destructor call with MSVC 2022, so the behaviour of the code generated with MSVC 2022 is undefined and it can potentially format your hard drive.
MSVC 2022 version: Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30709 for x86
EDIT9:
Figured out what happens. The destructor of UpdateTask is called twice with MSVC 2022, see updated code.
EDIT10:
From docs: The coroutine is suspended (its coroutine state is populated with local variables and current suspension point).
awaiter.await_suspend(handle) is called, where handle is the coroutine handle representing the current coroutine. Inside that function, the suspended coroutine state is observable via that handle, and it's this function's responsibility to schedule it to resume on some executor, or to be destroyed (returning false counts as scheduling)
Looks like it was a compiler bug, that is probably fixed in Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31106.2 for x86, at least now the output is:
UpdateTask constructor.
testNestedTimerAwait started.
UpdateTask constructor.
testTimerAwait started.
testTimerAwait finished.
testNestedTimerAwait finished.
UpdateTask destructor.
UpdateTask destructor.
Related
There is an example of switching to a different thread with C++20 coroutines:
#include <coroutine>
#include <iostream>
#include <stdexcept>
#include <thread>
auto switch_to_new_thread(std::jthread& out) {
struct awaitable {
std::jthread* p_out;
bool await_ready() { return false; }
void await_suspend(std::coroutine_handle<> h) {
std::jthread& out = *p_out;
if (out.joinable())
throw std::runtime_error("Output jthread parameter not empty");
out = std::jthread([h] { h.resume(); });
// Potential undefined behavior: accessing potentially destroyed *this
// std::cout << "New thread ID: " << p_out->get_id() << '\n';
std::cout << "New thread ID: " << out.get_id() << '\n'; // this is OK
}
void await_resume() {}
};
return awaitable{ &out };
}
struct task {
struct promise_type {
task get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
task resuming_on_new_thread(std::jthread& out) {
std::cout << "Coroutine started on thread: " << std::this_thread::get_id() << '\n';
co_await switch_to_new_thread(out);
// awaiter destroyed here
std::cout << "Coroutine resumed on thread: " << std::this_thread::get_id() << '\n';
}
int main() {
std::jthread out;
resuming_on_new_thread(out);
}
the coroutine starts on the main thread and switches to a newly created thread.
What is the right way to make it switch back to the main thread?
So the code below
task resuming_on_new_thread(std::jthread& out) {
std::cout << "Coroutine started on thread: " << std::this_thread::get_id() << '\n';
co_await switch_to_new_thread(out);
// awaiter destroyed here
std::cout << "Coroutine resumed on thread: " << std::this_thread::get_id() << '\n';
co_await switch_to_main_thread();
std::cout << "Coroutine resumed on thread: " << std::this_thread::get_id() << '\n';
}
would print
Coroutine started on thread: 139972277602112
New thread ID: 139972267284224
Coroutine resumed on thread: 139972267284224
Coroutine resumed on thread: 139972277602112
switch_to_new_thread actually creates a new thread, it doesn't switch to a new thread. It then injects code that resumes the coroutine in it.
To run code on a specific thread, you have to actually run code on that thread. To resume a coroutine, that specific thread has to run code that resume that coroutine.
Here you did it by creating a brand-new thread and injecting code that does a resume.
A traditional way to do stuff like this is with a message pump. The thread you want to participate has a message pump and a queue of events. It runs the events in order.
To make a specific thread run some code, you send a message to that queue of events with the instructions (maybe the actual code, maybe just a value) in it.
To this end, such an "event consuming thread" is more than a std::jthread or std::thread; it is a thread safe queue and some in the thread popping tasks off it an executing them.
In such a system, you'd move between threads by sending messages.
So you'd have a queue:
template<class T>
struct threadsafe_queue {
[[nodiscard]] std::optional<T> pop();
[[nodiscard]] std::deque<T> pop_many(std::optional<std::size_t> count = {}); // defaults to all
[[nodiscard]] bool push(T);
template<class C, class D>
[[nodiscard]] std::optional<T> wait_until_pop(std::chrono::time_point<C,D>);
void abort();
[[nodiscard]] bool is_aborted() const { return aborted; }
private:
mutable std::mutex m;
std::condition_variable cv;
std::deque<T> queue;
bool aborted = false;
auto lock() const { return std::unique_lock(m); }
};
of tasks:
using task_queue = threadsafe_queue<std::function<void()>>;
a basic message pump is:
void message_pump( task_queue& q ) {
while (auto f = q.pop()) {
if (*f) (*f)();
}
}
you'd then make two task_queues, one for your main thread and one for your worker thread. To switch to worker instead of creating a new jthread you'd:
workerq.push( [&]{ h.resume(); } );
and similarly to switch to the main
mainq.push( [&]{ h.resume(); } );
there are lots of details I have skipped over, but this is a sketch of how you'd do it.
One way to make this happen is to have a thread-safe queue that the coroutine places itself in to tell the main thread "please resume me now". At that point, you're basically building a thread pool. The main function has to watch that queue (poll it at regular intervals or wait for something to be placed in it), then fetch and execute an element (work item) once one is available.
below is a snippet testing empty coroutine playing with promise_type
#include <iostream>
#include <coroutine>
#define DEBUG std::cout << __PRETTY_FUNCTION__ << std::endl
struct TaskSuspendAll {
// must be of this name
struct promise_type {
TaskSuspendAll get_return_object() noexcept {
return TaskSuspendAll{
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
std::suspend_always initial_suspend() noexcept {
DEBUG;
return {};
}
std::suspend_always final_suspend() noexcept {
DEBUG;
return {};
}
void unhandled_exception() {}
void return_void() {
DEBUG;
}
};
std::coroutine_handle<promise_type> ch;
};
TaskSuspendAll TestSuspendAll() {
DEBUG;
co_return;
}
int main() {
std::cout << std::endl;
auto t = TestSuspendAll();
t.ch.resume();
//t.ch.resume()
//t.ch.destroy();
return 0;
}
running this I get
std::__n4861::suspend_always TaskSuspendAll::promise_type::initial_suspend()
TaskSuspendAll TestSuspendAll()
void TaskSuspendAll::promise_type::return_void()
std::__n4861::suspend_always TaskSuspendAll::promise_type::final_suspend()
My understanding is that co_await is applied to initial_suspend and final_suspend. When I call TestSuspendAll in the main function it will eventually call co_await promise.initial_suspend() and return to the caller given i have std::suspend_always awaitable. Then i resume the coroutine and the body gets executed. At some point, we will have co_await promise.final_suspend() and again return to the caller.
question: I would expect that i have to do a second call to resume coroutine so that co_await promise.final_suspend() succeeded and coroutine completed. However that causes seg fault. I know that it's undefined behavior calling resume on completed coroutine, however it's not completed 100% as far as I understand. My expectation was that final_suspend behaves the same as initial_suspend... what is the logic here? is that we have to use destroy after call to final_suspend?
thanks a lot for clarification!
VK
Being suspended at its final suspend point is the definition of a coroutine being done. Literally; that's what coroutine_handle::done returns. Attempting to resume such a coroutine is UB.
So your expectation is not correct.
I need something to suspend Lambdas in C++ and resume them. I try to narrow it down to a very simple example:
Lets assume I have a singleton class orchestrator where I can register a lambda:
int main() {
orchestrator::getInstance().registerLambda([&](){
// Do something:
...
wait(); // Suspend here
// When waked up continue here:
...
wait(); // Suspend here
...
});
orchestrator::start()
}
In the orchestrator class itself there is a main loop which calls then this lambda-function from time to time.
orchestrator::start()
{
while(true) {
lambda();
// Do other stuff:
...
}
}
I thought about co-routines but they seam to complex in my opinion. The solution should stick with the concept of lambda and standard C++. Modern C++ like '11, '17 or '20 would also be fine.
In the interest of the future where coroutine support will be more complete, here's one way a coroutine could look:
resumable foo() {
std::cout << "Starting foo\n";
while (true) {
co_await std::suspend_always{}; // Could be co_await wait(); if you prefer, if wait() returns suspend_always
std::cout << "Resuming foo\n";
}
}
int main() {
auto m = foo();
for (int i = 0; i < 5; ++i) {
std::cout << "Back in main to resume foo\n";
m();
}
std::cout << "Done main\n";
}
This outputs "Starting foo", followed by 5 back-and-forths between main and foo, and then "Done main". Using a lambda is trivial: specify resumable as the return type. (See the live example)
The messy part is defining resumable, and this part belongs in a library. I'd say it's a good candidate for the standard library in some form after some more common types like task and generator. In fact, this type is basically a generator<void> with a different iteration API. Without using a library, it's not too bad, but note that I haven't bothered to do things like define what happens if you try to resume the lambda after it's done:
class resumable {
std::coroutine_handle<> _coro;
explicit resumable(std::coroutine_handle<> h) noexcept
: _coro(h) {}
public:
// All fluff except for giving resumable a coroutine handle
struct promise_type {
resumable get_return_object() noexcept { return resumable(std::coroutine_handle<promise_type>::from_promise(*this)); }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() noexcept {}
void unhandled_exception() noexcept {}
};
// This is how the caller interacts, they just call this object repeatedly.
void operator()() const noexcept {
_coro.resume();
}
};
If not using coroutines, it's back to good old state machines:
struct state_resumable {
// TODO: Store all state
state_resumable() {
std::cout << "Starting resumable\n";
}
void operator()() {
// TODO: Figure out what to execute next based on the stored state
std::cout << "Resuming resumable\n";
}
};
int main() {
auto m = state_resumable();
for (int i = 0; i < 5; ++i) {
std::cout << "Back in main to resume resumable\n";
m();
}
std::cout << "Done main\n";
}
What isn't shown here is the effort required to manually keep track of state. Coroutines automatically store away the local variables in your function and restore them when the coroutine is resumed, plus keep track of which part of the function to execute next. With a state machine, you have to do all of these yourself. You cannot use a single lambda as above because only coroutines can actually suspend mid-execution. With a state machine, you're pretending to do this, but the function must actually finish completely each time.
I want to create a container that I can push functions into, that will instantly be started in a thread. Once the function is complete, it should automatically be removed from the container so that the container does not grow indefinitely.
Here is my attempt so far:
#include <thread>
#include <future>
#include <iostream>
#include <map>
class j_thread {
std::thread thread;
public:
j_thread() {}
template<typename F>
j_thread(const F& f) : thread(f) {}
j_thread& operator = (j_thread&& other) {
this->thread.swap(other.thread);
return *this;
}
virtual ~j_thread() {
thread.join();
}
};
class jobs {
std::map<size_t, j_thread> threads;
public:
template<typename F>
void add_job(const F &function) {
size_t job_id = threads.size();
auto wrapped_function = [&function, job_id, this]() {
function();
threads.erase(job_id);
};
threads[job_id] = j_thread(wrapped_function);
}
void wait_for_all() {
while(threads.size() != 0) {}
}
};
int main() {
jobs j;
j.add_job([](){std::cout << "hello" << std::endl;});
j.add_job([](){std::cout << "world" << std::endl;});
j.wait_for_all();
}
But when run gives the error:
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument
hello
terminate called recursively
12:15:44: The program has unexpectedly finished.
Calling join within the body of thread is undefined behaviour.
Look at error conditions for join:
Error Conditions resource_deadlock_would_occur if this->get_id() ==
std::this_thread::get_id() (deadlock detected)
Your body is:
auto wrapped_function = [&function, job_id, this]() {
function();
threads.erase(job_id);
};
where you call erase, dtor of jthread is being called which calls join on joinable thread.
Instead of join, in dtor you should call detach.
To avoid dangling reference function must be captured by value.
Also you have to add some mutex to avoid data race on map, when calling size and erase:
std::mutex m;
int size() {
std::lock_guard<std::mutex> lock{m};
return threads.size();
}
auto wrapped_function = [f = function, job_id, this]() {
f();
std::lock_guard<std::mutex> l(m);
threads.erase(job_id);
};
void wait_for_all() {
while(size() != 0) {}
}
Demo
In the C++ coroutines TS (2017), there is an example of an awaitable object.
template <class Rep, class Period>
auto operator co_await(std::chrono::duration<Rep, Period> d) {
struct awaiter {
std::chrono::system_clock::duration duration;
...
awaiter(std::chrono::system_clock::duration d) : duration(d){}
bool await_ready() const { return duration.count() <= 0; }
void await_resume() {}
void await_suspend(std::experimental::coroutine_handle<> h){...}
};
return awaiter{d};
}
using namespace std::chrono;
my_future<int> h();
my_future<void> g() {
std::cout << "just about go to sleep...\n";
co_await 10ms;
std::cout << "resumed\n";
co_await h();
}
Like a typical StackOverflow Question, it will not compile. After cursing quietly for a while, I decided to turn it into a [MCVE] -- for learning. The code below compiles and runs on VC++17 with /await enabled. I think it probably does approximately what the TS authors intended. Alas, it employs a detached thread. It is not easy to see how that thread could be harvested via join or future::get or signal_all_at_thread_exit() or ...
For example, join cannot be added to a destructor for awaiter. In the spawned thread, h.resume() causes the awaiter object to be moved into the spawned thread and its (default) constructor called there. So the destructor is called in a different thread than the constructor.
The question, aside from "Is this what the TS intended?", is "Can this be improved, in a reasonably economical way, to tend to the dangling thread?" (And if so how?)
#include <experimental/coroutine>
#include <future>
#include <thread>
namespace xtd = std::experimental;
template <class Rep, class Period>
auto operator co_await(std::chrono::duration<Rep, Period> dur) {
struct awaiter {
using clock = std::chrono::high_resolution_clock;
clock::time_point resume_time;
awaiter(clock::duration dur) : resume_time(clock::now()+dur) {}
bool await_ready() { return resume_time <= clock::now(); }
void await_suspend(xtd::coroutine_handle<> h) {
std::thread([=]() {
std::this_thread::sleep_until(resume_time);
h.resume(); // destructs the obj, which has been std::move()'d
}).detach(); // Detach scares me.
}
void await_resume() {}
};
return awaiter{ dur };
}
using namespace std::chrono;
std::future<int> g() {
co_await 4000ms;
co_return 86;
}
template<typename R>
bool is_ready(std::future<R> const& f)
{ return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }
int main() {
using std::cout;
auto gg = g();
cout << "Doing stuff in main, while coroutine is suspended...\n";
std::this_thread::sleep_for(1000ms);
if (!is_ready(gg)) {
cout << "La lala, lala, lala...\n";
std::this_thread::sleep_for(1500ms);
}
cout << "Whew! Done. Getting co_return now...\n";
auto ret = gg.get();
cout << "coroutine resumed and co_returned " << ret << '\n';
system("pause");
return ret;
}
Can this be improved, in a reasonably economical way, to tend to the dangling thread?
You can use "thread pool" implementation, instead of on-demand detached thread.
Here is toy example:
https://gist.github.com/yohhoy/a5ec6d4aeeb4c60d3e4f3adfd1df9ebf