'Persistent storage in Lambda C++
I am wanting to create a lambda that can store intermediate data between calls. Is this something that is possible? I never hit the Tokens Persisted even after the Storing Tokens is printed.
auto process = [&in, &out, callback, tokens = std::deque<O>{}]() mutable {
// Debug this is never reached
if (!tokens.empty()) {
printf("Tokens Persisted: %u\n",token.size());
}
// Populate with new tokens
if(tokens.empty() && in.ReadValid()) {
auto new_tokens = callback(in.Read());
tokens.insert(tokens.begin(), new_tokens.begin(), new_tokens.end());
}
// Drain the tokens
while (!tokens.empty() && out.WriteValid()) {
out.Write(tokens.front());
tokens.pop_front();
}
// Debug should be hit if we couldn't all write tokens out
if (!tokens.empty()) {
printf("Storing Tokens %u\n", tokens.size());
}
};
Solution 1:[1]
I achieved what I wanted by creating a shared_ptr and passing that into the process lambda. This allows each creation of process to have a unique buffer that it can store results in case of back pressure from the out.
auto t = std::make_shared<std::deque<O>>();
auto process = [&in, &out, callback, tokens = t]() mutable {
// Debug this is never reached
if (!tokens->empty()) {
printf("Tokens Persisted: %u\n",token->size());
}
// Populate with new tokens
if(tokens->empty() && in.ReadValid()) {
auto new_tokens = callback(in.Read());
tokens->insert(tokens->begin(), new_tokens.begin(), new_tokens.end());
}
// Drain the tokens
while (!tokens->empty() && out.WriteValid()) {
out.Write(tokens->front());
tokens->pop_front();
}
// Debug should be hit if we couldn't all write tokens out
if (!tokens->empty()) {
printf("Storing Tokens %u\n", tokens->size());
}
};
Solution 2:[2]
Another solution, use mutable lambda (no static var required is better for multithread) :
#include <iostream>
auto callable = [state = 4]() mutable ->int{
std::cout<<"state: "<<state<<std::endl;
++state;
return state;
};
int main()
{
callable();//prints 4
callable();//prints 5
callable();//prints 6
}
Solution 3:[3]
You can make use of static variable to store the state that you want to preserve between different calls, as shown below. A simplified example(since your original example is unnecessarily complicated for the purpose of the question) is given below:
auto callable = []()->int{
static int state = 4;
std::cout<<"state: "<<state<<std::endl;
++state;
return state;
};
int main()
{
callable();//prints 4
callable();//prints 5
callable();//prints 6
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1020788 |
| Solution 2 | jls28 |
| Solution 3 | Anoop Rana |
