Home > Mobile >  Persistent storage in Lambda C
Persistent storage in Lambda C

Time:04-15

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());
            }
        };

CodePudding user response:

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  
}

CodePudding user response:

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
   
}
  • Related