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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:14:32+00:00 2026-06-03T05:14:32+00:00

Suppose we have multiple threads all calling the same function: def foo # do

  • 0

Suppose we have multiple threads all calling the same function:

def foo 
  # do stuff ...
end

100.times do |i|
  Thread.new do
    foo
  end
end

If two or more threads are currently inside of foo, do they each share the same local variables within foo?

This relates to my second question. Do threads have individual stack frames, or do they share stack frames within a single process? Specifically, when multiple threads each invoke foo and before foo returns, are there multiple copies of foo on the stack, each with their own local variables, or is there only one copy of foo on the stack?

  • 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-03T05:14:34+00:00Added an answer on June 3, 2026 at 5:14 am

    Yes, they share the same variables. This is a key element of Threads and is fine in a read-only context, but if they write to any of those variables, you need to use a Mutex and synchronize the threads, so only one can be changing a variable at any given time. Sometimes they may be invoking a method which changes data indirectly, so you need to know the system fully before you decide if you need to synchronize or not.

    As for your second question, if I understand what you’re asking, they have individual stack frames, but they are still all sharing the same data in memory.

    The clarify, in the following example, the local variable zip is shared by multiple threads, since it was defined in the current scope (threads don’t change scope, they just start a separate, parallel thread of execution in the current scope).

    zip = 42
    
    t = Thread.new do
      zip += 1
    end
    
    t.join
    
    puts zip # => 43
    

    The join here saves me, but obviously there’s no point in the thread at all, if I keep that there. It would be dangerous if I were to do the following:

    zip = 42
    
    t = Thread.new do
      zip += 1
    end
    
    zip += 1
    
    puts zip # => either 43 or 44, who knows?
    

    That is because you basically have two threads both trying to modify zip at the same time. This becomes noticeable when you are accessing network resources, or incrementing numbers etc, as in the above.

    In the following example, however, the local variable zip is created inside a an entirely new scope, so the two threads aren’t actually writing to the same variable at the same time:

    def foo
      zip = 42
      zip += 1 # => 43, in both threads
    end
    
    Thread.new do
      foo
    end
    
    foo
    

    There are two parallel stacks being managed, each with their own local variables inside the foo method.

    The following code, however, is dangerous:

    @zip = 42 # somewhere else
    
    def foo
      @zip += 1
    end
    
    Thread.new do
      foo
    end
    
    foo
    
    puts @zip # => either 43 or 44, who knows?
    

    That’s because the instance variable @zip is accessible outside of the scope of the foo function, so both threads may be accessing it at the same time.

    These problems of ‘two threads changing the same data at the same time’ are resolved by using carefully placed Mutexes (locks) around the sections of the code that change the variable. The Mutex must be created before the threads are created, because in the case of a Mutex, it is (by design) vital that both threads access the same Mutex, in order to know if it’s locked or not.

    # somewhere else...
    @mutex = Mutex.new
    @zip   = 42
    
    def foo
      @mutex.synchronize do
        @foo += 1
      end
    end
    
    Thread.new do
      foo
    end
    
    foo
    
    puts @zip # => 44, for sure!
    

    If when the flow of execution reaches the Mutex#synchronize line, it tries to lock the mutex. If successful, it enters the block and continues executing. Once the block finishes, the mutex is unlocked again. If the mutex is already locked, the thread waits until it becomes free again… effectively it’s like a door that only one person can walk through at a time.

    I hope this clears things up.

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

Sidebar

Related Questions

Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
Suppose multiple Modal Windows shown above each other. All of those have ShowInTaskbar =
In Windows, suppose you have multiple windows (HWNDs) of the same window class open.
Suppose we have a map that is shared between multiple threads. It represents a
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
Using Hashtable I want to have multiple objects mapped to the same key. E.g.
Suppose I have a custom collection class that provides some internal thread synchronization. For
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
I have 3 questions about thread and process communication. Can the Linux function msgget(),
Suppose I have multiple roles, each one defining a set of items: package A;

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.