Skip to content

Instantly share code, notes, and snippets.

@stoyannk
Last active May 7, 2019 15:25
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/e73e248815631a903c2682971162b1b0 to your computer and use it in GitHub Desktop.
Save stoyannk/e73e248815631a903c2682971162b1b0 to your computer and use it in GitHub Desktop.
Task system sample
static const std::filesystem::path testHTML{"../test_res/index.html"};
static const std::filesystem::path testCSS{"../test_res/style.css"};
using DataBuffer = std::unique_ptr<std::uint8_t[]>;
struct Document
{
DataBuffer HTMLData;
DataBuffer CSSData;
std::mutex DataMut;
void MakeDOM()
{
std::lock_guard<std::mutex> l{DataMut};
assert(HTMLData && CSSData);
// Pretend we do something useful here...
std::cout << "Document created" << std::endl;
}
};
void StartCreateDocument()
{
std::shared_ptr<Document> result = std::make_shared<Document>();
Enqueue(Work, [=]()
{
std::lock_guard<std::mutex> l{result->DataMut};
result->HTMLData = std::move(ReadFile(testHTML));
OnDocumentResourceLoaded(result);
});
Enqueue(Work, [=]()
{
std::lock_guard<std::mutex> l{result->DataMut};
result->CSSData = std::move(ReadFile(testCSS));
OnDocumentResourceLoaded(result);
});
}
void OnDocumentResourcesLoaded(std::shared_ptr<Document>& result)
{
if (result->HTMLData && result->CSSData)
{
Enqueue(Main, [=]()
{
result->MakeDOM();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment