Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:51:42+00:00 2026-06-02T23:51:42+00:00

I have a program, where I cannot use the standard std::async and threading mechanisms.

  • 0

I have a program, where I cannot use the standard std::async and threading mechanisms. Instead I have to code the program like so:

void processor( int argument, std::function<void(int)> callback ) {
  int blub = 0;

  std::shared_ptr<object> objptr = getObject();

  // Function is called later.
  // All the internal references are bound here!
  auto func = [=, &blub]() {
    // !This will fail since blub is accessed by reference!
    blub *= 2;

    // Since objptr is copied by value it works.
    // objptr holds the value of getObject().
    objptr->addSomething(blub);

    // Finally we need to call another callback to return a value
    callback(blub);
  };

  objptr = getAnotherObject();

  // Puts func onto a queue and returns immediately.
  // func is executed later.
  startProcessing(func);
}

I now would like to know whether I am doing it right or what the best way of using lambdas as asynchronous callbacks is.

EDIT: Added expected behavior to the code comments.
See answer/comments for possible solutions for the problem with blub.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:51:43+00:00Added an answer on June 2, 2026 at 11:51 pm

    The function object will contain a reference to the local variable blub. As in every other situation in the language, this won’t make the local variable live after the function ends.

    Copies of all the other captured objects will be stored within the function object, since they’re captured-by-value. This means there’s no issue with them.

    If you want it to live after the function ends, you cannot tie its lifetime to the function: you need dynamic storage duration. A std::unique_ptr can be used to to handle the cleanup of such an object, but it gets a bit annoying because you can’t “capture-by-move” into a lambda :S

    auto blub = make_unique<int>(0); // [1]
    
    std::shared_ptr<object> objptr = getObject();
    
    // use std::bind to store the unique_ptr with the lambda
    auto func = std::bind([=](std::unique_ptr<int>& blub) {
      *blub *= 2;
    
      objptr->addSomething(*blub);
    
      callback(*blub);
    }, std::move(blub)); // move the unique_ptr into the function object
    
    objptr = getAnotherObject();
    
    // func is not copiable because it holds a unique_ptr
    startProcessing(std::move(func)); // move it
    

    As an added note, the old deprecated std::auto_ptr would actually work fine here, because if the lambda captures it by value it gets copied and its strange copy semantics are exactly what’s needed.


    1. See GOTW #102 for make_unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I have a program which I would like to run every X min the
I have a program that looks something like this: public partial class It {
I have a particular problem, I have some program that I cannot modify but
I have program that has a variable that should never change. However, somehow, it
I have program, that must interact with a console program before my program can
I have program that runs fast enough. I want to see the number of
I have a program that works with a variety of files on both the
I have a program that only allows one instance of itself to run. I
I have a program that receives real time data on 1000 topics. It receives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.