The Closure Outlives The Frame
This queue runs each job later, from a worker thread.
#include <functional>
#include <string>
#include <vector>
void log(const std::string& tag, int n);
void run(std::vector<std::function<void()>>& jobs);
std::vector<std::function<void()>> jobs;
void enqueue(const std::string& name, int retries) {
std::string tag = "job:" + name;
jobs.push_back([&tag, retries] {
log(tag, retries);
});
}
int main() {
enqueue("index", 3);
enqueue("compact", 1);
run(jobs); // jobs invoked here
}
It works in a debug build and prints garbage tags in release. What is wrong?
Sign in to answer questions and track your progress
Sign In