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 8274823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:52:11+00:00 2026-06-08T07:52:11+00:00

Is the following function thread-safe? And if it’s not thread-safe, then is there really

  • 0

Is the following function thread-safe? And if it’s not thread-safe, then is there really any overhead in making that funImpl non-static? Or does the compiler actually inline that function-object function and skip creating the function object altogether?

int myfun(std::array<int, 10> values)
{
    static const auto funImpl = [&]() -> int
    {
        int sum = 0;

        for (int i = 0; i < 10; ++i)
        {
            sum += values[i];
        }
        return sum;
    };

    return funImpl();
}

EDIT:
I edited the function signature from:

int myfun(const std::array<int, 10>& values)

to:

int myfun(std::array<int, 10> values)

so that it is clear that I’m not asking about the tread-safety of values, but the thread-safety of the function local static variable funImpl.

  • 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-08T07:52:12+00:00Added an answer on June 8, 2026 at 7:52 am

    Not only is it not thread safe, it doesn’t do what you want.

    By defining the lambda as static, it captures (by reference) the array passed in the first time it’s called. Further calls continue to reference the original array, regardless of which array is passed in.

    When the first array goes out of scope, further calls will invoke UB, as it now has a dangling reference.

    Edit: An example http://ideone.com/KCcav

    Note, even if you captured by value, you’d still have a problem because it would still only capture the first time you invoke the function. You wouldn’t have the dangling pointer, but it still would only initialize the copy the first time.

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

Sidebar

Related Questions

I was reading this: http://en.wikipedia.org/wiki/Thread_safety Is the following function thread-safe? void foo(int y){ int
I have a web server that is running PHP 5.3.6 non thread safe (VC9),
Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value
I have the following function call from a thread: Thread Move = new Thread(){
The following function rewrites urls from news and product titles that contain all sorts
The following function that reports the current line-number (and time) which I would like
I'm currently using the following code for thread-safe access of a variable. int gnVariable;
I'm working on a thread-safe collection that uses Dictionary as a backing store. In
I have a class which is not thread safe: class Foo { /* Abstract

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.