You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is only a brainstorm. This paragraph talks about the possibility instead of the implemention-detail of it
Idea
As is known to all, <iostream> is a successful and widely-used abstraction of sync-io.
I'm thinking a kind of async_iostream, which provides interfaces like this (let's begin with an example like std::fstream):
auto async_stream = std::async_ofstream("file.txt"); // Derived from std::ostream.
async_stream << "Hello, world" << std::endl; // -> Returns a `std::execution::sender`
Meanwhile all algorithms on synchronous std::iostream is keeped, for example:
async_stream << "Explicit write this line to file"
<< std::flush
<< "Lazily write this line, e.g. when cache is full or next time you flush it";
stdexec::sync_wait(async_stream);
The "operator<<" overload exactly in same way as synchronous std::iostream, like this:
std::ostream& operator<< (std::ostream& os, const student& stu) /* for both sync or async*/
{
return os << stu.name() << stu.age() << stu.introduction(); // If stream is async then returns a sender.
}
std::ostream (sync) itself is a sender (maybe template-specialized?), sync_wait it has not effect.
std::this_thread::sync_wait(ofstream << "Hello") has same effect as the one without sync_wait,
std::this_thread::sync_wait(async_ofstream << "Hello") actually returns a lazy sender (which works like std::this_thread::sync_wait(boost::asio::socket().async_send("Hello", use_sender))).
Maybe we can add some algorithms like this:
auto stream = std::ofstream("file.txt"); // Sync.
stream << std::become_async // It become a `std::async_ofstream&`
<< "data"
<< std::become_sync // It become a `std::sync_ofstream&`
<< "HOoOoOoOoO";
Pros
It's simple. The first lesson of C++ in many of us is like this:
It only make cout to something async, which does not break any grammar or expression. Everyone can easily get started to async-io, without learning anything more than their first class of C++. It is simple.
It's clear-expressable. We can create something like this:
// Assume we have std::tcpstream, which has same interface like std::fstream.// The client send: read ./file_1// This server do: (send all bytes in ./file_1 to client).// The client send: write ./file_2// This server do: (write "my-file-data" into file).
std::execution::sender autoserver (std::tcp_stream& stream) {
return stream >> operation >> filename
| std::execution::let_value([]
{
if (operation == "read")
returnstd::ranges::copy(std::views::istream(filename), std::ostream_iterator<char>(stream));
elseif (operation == "write")
returnstd::async_fstream(filename) << "my-file-data";
});
}
Cons
Lifetime. Imagine this case:
std::async_ofstream << 42 << string << huge_eigen_matrix;
// And after this line, `huge_eigen_matrix` destructs.
The possible ways are:
std::execution, okay we std::execution::just these values. But most times we just(std::move(values...)), and here
implicit std::move huge_eigen_matrix might be inproper (?),
explicit std::move huge_eigen_matrix (like std::cout << std::move(matrix)) does not keep grammar same as sync-iostrema.
Boost.Asio, oh we only accept boost::asio::buffer (something similar to std::span), i asio dont care about the lifetime.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is only a brainstorm. This paragraph talks about the possibility instead of the implemention-detail of it
Idea
As is known to all,
<iostream>is a successful and widely-used abstraction of sync-io.I'm thinking a kind of
async_iostream, which provides interfaces like this (let's begin with an example likestd::fstream):Meanwhile all algorithms on synchronous std::iostream is keeped, for example:
The "operator<<" overload exactly in same way as synchronous std::iostream, like this:
std::ostream(sync) itself is a sender (maybe template-specialized?),sync_waitit has not effect.std::this_thread::sync_wait(ofstream << "Hello")has same effect as the one withoutsync_wait,std::this_thread::sync_wait(async_ofstream << "Hello")actually returns a lazy sender (which works likestd::this_thread::sync_wait(boost::asio::socket().async_send("Hello", use_sender))).Maybe we can add some algorithms like this:
Pros
It only make
coutto something async, which does not break any grammar or expression. Everyone can easily get started to async-io, without learning anything more than their first class of C++. It is simple.Cons
The possible ways are:
std::execution, okay westd::execution::justthese values. But most times wejust(std::move(values...)), and herehuge_eigen_matrixmight be inproper (?),huge_eigen_matrix(likestd::cout << std::move(matrix)) does not keep grammar same as sync-iostrema.Boost.Asio, oh we only acceptboost::asio::buffer(something similar tostd::span), i asio dont care about the lifetime.Beta Was this translation helpful? Give feedback.
All reactions