How do two or more threads share memory on the heap that they have allocated?

前端 未结 5 548
春和景丽
春和景丽 2020-12-28 08:09

As the title says, how do two or more threads share memory on the heap that they have allocated? I\'ve been thinking about it and I can\'t figure out how they can do it. Her

相关标签:
5条回答
  • 2020-12-28 08:48

    My interpretation of your question: How can thread A get to know a pointer to the memory B is using? How can they exchange data?

    Answer: They usually start with a common pointer to a common memory area. That allows them to exchange other data including pointers to other data with each other.

    Example:

    1. Main thread allocates some shared memory and stores its location in p
    2. Main thread starts two worker threads, passing the pointer p to them
    3. The workers can now use p and work on the data pointed to by p

    And in a real language (C#) it looks like this:

    //start function ThreadProc and pass someData to it
    new Thread(ThreadProc).Start(someData)
    

    Threads usually do not access each others stack. Everything starts from one pointer passed to the thread procedure.


    Creating a thread is an OS function. It works like this:

    1. The application calls the OS using the standard ABI/API
    2. The OS allocates stack memory and internal data structures
    3. The OS "forges" the first stack frame: It sets the instruction pointer to ThreadProc and "pushes" someData onto the stack. I say "forge" because this first stack frame does not arise naturally but is created by the OS artificially.
    4. The OS schedules the thread. ThreadProc does not know it has been setup on a fresh stack. All it knows is that someData is at the usual stack position where it would expect it.

    And that is how someData arrives in ThreadProc. This is the way the first, initial data item is shared. Steps 1-3 are executed synchronously by the parent thread. 4 happens on the child thread.

    0 讨论(0)
  • 2020-12-28 08:55

    Threads can share memory on a heap if they both use the same heap. By default most languages/frameworks have a single default heap that code can use to allocate memory from the heap. In unmanaged languages you generally make explicit calls to allocate heap memory. In C, that might be malloc, etc. for example. In managed languages heap allocation is usually automatic and how allocation is done depends on the language--usually through the use of the new operator. but, that depends slightly on context. If you provide the OS or language context you're asking about, I might be able to provide more detail.

    0 讨论(0)
  • 2020-12-28 08:57

    A really short answer from a bird's view (1000 miles above):
    Threads are execution paths of the same process, and the heap actually belongs to the process (and as a result shared by the threads). Each threads just needs its own stack to function as a separate unit of work.

    0 讨论(0)
  • 2020-12-28 09:10

    A Thread shared with other threads belonging to the same process: its code section, data section and other operating system resources such as open files and signals.

    0 讨论(0)
  • 2020-12-28 09:11

    The part you are missing is static memory containing static variables.

    This memory is allocated when the program is started, and assigned known adresses (determined at the linking time). All threads can access this memory without exchanging any data runtime, because the addresses are effectively hardcoded.

    A simple example might look like this:

    // Global variable.
    std::atomic<int> common_var;
    
    void thread1() {
      common_var = compute_some_value();
    }
    
    void thread2() {
      do_something();
      int current_value = common_var;
      do_more();
    }
    
    

    And of course the global value may be a pointer, that can be used to exchange heap memory. The producer allocates some objects, the consumer takes and uses them.

    // Global variable.
    std::atomic<bool> produced;
    SomeData* data_pointer;
    
    void producer_thread() {
      while (true) {
        if (!produced) {
          SomeData* new_data = new SomeData();
          data_pointer = new_data;
          // Let the other thread know there is something to read.
          produced = true;
        }
      }
    }
    
    void consumer_thread() {
      while (true) {
        if (produced) {
          SomeData* my_data = data_pointer;
          data_pointer = nullptr;
          // Let the other thread know we took the data.
          produced = false;
          do_something_with(my_data);
          delete my_data;
        }
      }
    }
    

    Please note: these are not examples of good concurrent code, but they show the general idea without too much clutter.

    0 讨论(0)
提交回复
热议问题