Inside the companion
Renderer Engineering
Explore bounded image work, allocator lifetimes, cancellation, and Asio coroutines.
Project files 15
Run the blocking baseline
Submit a small checksum batch and inspect its completion and publication policy.
#include "prism_engineering/worker_pool.hpp"
#include <iostream>
#include <thread>
int main() {
using namespace prism_engineering;
try {
auto completed =
run_batch({{7, {1, 2, 3}, false}, {6, {10, 20}, false}}, {2, {}});
if (!completed)
return 1;
std::cout << "Generation 7 checksum: " << completed->results[0].checksum
<< "\nOld generation accepted: " << std::boolalpha
<< accepts(completed->results[1], 7) << '\n';
auto gate = std::make_shared<WorkGate>();
std::stop_source cancellation;
// The controller owns its capture values; no coroutine or detached task.
std::jthread controller{[gate, cancellation](std::stop_token stop) mutable {
if (gate->wait_for_arrivals(2, stop))
cancellation.request_stop();
}};
const auto cancelled =
run_batch({{8, {1, 2, 3}, false}}, {2, gate}, cancellation.get_token());
controller.request_stop(); // Also releases the controller if thread launch
// failed.
controller.join();
if (!cancelled || cancelled->results[0].status != Status::cancelled)
return 1;
std::cout
<< "Cancelled before computation; workers joined before return.\n";
return 0;
} catch (...) {
std::cerr << "Engineering experiment could not complete.\n";
return 1;
}
}
This is the file included with the course. Copy and download give you its complete source.
Taking your work offline? Download all course companions
