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

  • Home
  • SEARCH
  • 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 8535559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:27:37+00:00 2026-06-11T10:27:37+00:00

I have a lambda inside a for loop with the loop variable parameter in

  • 0

I have a lambda inside a for loop with the loop variable parameter in the lambda. When I run it, I expect the numbers 0-9 to be output. But since it is a lambda, the x doesn’t get evaluated immediately.

for (int x = 0; x < n; ++x)
{
    vec.push_back(thread{[&x]() {
        m.lock();
        cout << x << endl;
        m.unlock();
    }});
}

Output:

0
3
3
9
etc.

The solution for other languages would be to create a temporary variable,

for (int x = 0; x < n; ++x)
{
    int tmp = x;
    vec.push_back(thread{[&tmp]() {
        m.lock();
        cout << tmp << endl;
        m.unlock();
}});
}

but that doesn’t seem to work.

see Threads receiving wrong parameters

Bonus:

In my search for an answer, I stumbled upon this question
Generalizing C++11 Threads class to work with lambda
which recommends not using a container that would invalidate the iterators.
Why would that be?

  • 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-11T10:27:38+00:00Added an answer on June 11, 2026 at 10:27 am

    When you specify the capture, you can choose between capture by value and capture by reference. You have chosen to capture by reference. Capturing by reference means that the variable inside the lambda function is referring to the same object. The implication is that any changes to this variable will be shared and you also need to make sure that the referenced object stays around for the life-time of the lambda function.

    You probably meant to capture by values. To do this, you can either replace the capture specification to become [=] or to become [x]. The latter makes sure that only x can be accessed while the former would allow other variables to be accessible.

    BTW, I’d recommend not using lock() and unlock() explicitly but rather use one of the lock guards. With this, the body of your loop would look something like this:

    vec.push_back(std::thread{[x](){
        std::lock_guard<std::mutex> kerberos(m);
        std::cout << x << "\n";
    }});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple lambda called inside async. But it gives a system_error. #include<future>
I have a following simple code: def get(): return [lambda: i for i in
I'm new with boost. I have a program which uses dynamic_bitset inside a lambda
Possible Duplicate: C#: using the iterator variable of foreach loop in a lambda expression
I have some data inside this variable and I want to know how could
So, every time I have written a lambda expression or anonymous method inside a
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
I have the following lambda function: f = lambda x: x == None and
I have not used many lambda expressions before and I ran into a case
I have a code in C# which uses lambda expressions for delegate passing to

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.