Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has the C++1y standard considered to support coroutine/goroutine?

I think coroutine/goroutine is very useful in case of a lot of concurrent small tasks must be performed quickly. Current std::thread cannot meet the requirements, because of its heavy cost.

I also think coroutine/goroutine cannot be supported simply through a C++ library, it should be implemented directly by the language's core features. Because coroutine/goroutine has a special semantics, which doesn't have a corresponding concept in current C++ standard.

Provided that we add a new keyword cppgo, then we may write code as follows:

void f(int n)
{
    ...
}

int main()
{
    for (int i = 0; i < 10000; ++i)
    {
        cppgo f(i);
    }
}

How cool it would be!

Has the C++1y standard considered to support coroutine/goroutine?

like image 502
xmllmx Avatar asked Feb 13 '23 13:02

xmllmx


1 Answers

There are several proposals related to coroutines but they don't target C++14 but a later version. Here are a few of these proposals:

  • Resumable functions (language feature, introduce resumable functions and await instruction/expression(?))
  • A proposal to add coroutines to the C++ standard library (library, based on Boost.Coroutine)

There are other more or less related papers (look at concurrency related papers and future related papers and you'll see a tendency toward allowing coroutines).

Some people also discuss the possibility to have a general syntax for "unwrapping monads" (like the await keyword proposed in 1 ) which would work both for std::future and other non-concurrency related types like optional or expected. Such features would allow to seamlessly apply the same algorithms to both resumable and not resumable functions, with or without concurrency being involved. In theory at least.

Here is a list of discussions related to these coroutine and monad concepts, by people doing and criticizing these proposals:

  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!topic/std-proposals/KCqwEq49GMA
  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/monad/std-proposals/5YT_bMfXLIM/GjncYChUrl0J
  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/monad/std-proposals/KCqwEq49GMA/a5f117Z5yZkJ
  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/monad/std-proposals/sL6zoPI-qEk/G_V7d1NzWnQJ
  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/resumable/std-proposals/LA8gxi73XYw/ilUxUO77Z9cJ
  • https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/resumable/std-proposals/sL6zoPI-qEk/G_V7d1NzWnQJ
like image 63
Klaim Avatar answered Feb 16 '23 03:02

Klaim