Skip to content

Instantly share code, notes, and snippets.

@stoyannk
Created May 5, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoyannk/4c451cd70730885f07197f99d2796896 to your computer and use it in GitHub Desktop.
Save stoyannk/4c451cd70730885f07197f99d2796896 to your computer and use it in GitHub Desktop.
Experiment with coroutines and cppcoro
using namespace cppcoro;
static const std::filesystem::path testCSS{"../test_res/style.css"};
static const std::filesystem::path testHTML{"../test_res/index.html"};
using DataBuffer = std::unique_ptr<std::uint8_t[]>;
task<DataBuffer> ReadFile(const std::filesystem::path& p)
{
DataBuffer result = std::make_unique<std::uint8_t[]>(len);
//Actual file loading happens here...
co_return result;
}
struct Document
{
DataBuffer HTMLData;
DataBuffer CSSData;
void MakeDOM()
{
std::cout << "Document created" << std::endl;
}
};
task<Document> CreateDocument(static_thread_pool& pool)
{
std::cout << "Creating document on thread " << std::this_thread::get_id() << std::endl;
auto readHTML = ReadFile(testHTML);
auto readCSS = ReadFile(testCSS);
Document result;
std::tie(result.HTMLData, result.CSSData) = std::move(co_await when_all(
schedule_on(pool, std::move(readHTML)),
schedule_on(pool, std::move(readCSS))));
result.MakeDOM();
co_return result;
}
Document BuildDocument(static_thread_pool& pool)
{
return sync_wait(CreateDocument(pool));
}
int main()
{
static_thread_pool pool;
auto document = BuildDocument(pool);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment