Last active
May 7, 2019 15:25
-
-
Save stoyannk/e73e248815631a903c2682971162b1b0 to your computer and use it in GitHub Desktop.
Task system sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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