-1

In multithreading (with std::thread for example), the process memory is shared between threads. However, does this extend to a function being called with two different arguments?

A silly example:

int add_two_in_ridiculous_fashion(int x) {
    x++;
    std::this_thread::sleep(1s);
    x++;
    return x;
}

While the thread is sleeping, another thread rolls in and replaces x with a different value, and the two threads return the latter's value + 1 and + 2 respectively. Is this possible?

If yes, is there a lock-free approach of preventing this in C++?

Timangar
  • 32
  • 6
  • Nope, this is not going to happen, with the shown code. Thankfully, C++ does not work this way. – Sam Varshavchik Nov 11 '21 at 18:07
  • Local variables, including function parameters, are local and not shared between threads. This is implemented by giving each thread a separate region of memory for its stack. Your function will return `x+2` in every instance. – Nate Eldredge Nov 11 '21 at 18:08
  • I think the _real_ question behind this question is "What is a stack variable?" This post seems to assume a misunderstanding about variables on a stack. – Drew Dormann Nov 11 '21 at 18:13
  • "the process memory is shared" : you should read about the different blocks of memory a process uses and if they are or not shared. – pasaba por aqui Nov 11 '21 at 18:41
  • I could see this being hypothetically possible on some crude OS that doesn't use protected memory where a thread could overwrite even stack memory from another thread without producing a segfault. I think for the most part to simplify though, the answer is no. I have a difficult time with simplifying communication trying to account for every possible case, however obscure and unlikely. –  Nov 11 '21 at 18:43

2 Answers2

2

No, this is not going to cause any problems. Each thread gets its own stack, and local variables and arguments (the x in your case) are on the stack and therefore separate for each thread. Member variables of a class or global variables are shared across different threads (for the same object instance).

PMF
  • 14,535
  • 3
  • 23
  • 49
2

Variables with automatic storage - such as the parameter x - are distinct each time their scope is entered - in this case each time the function is called.

This is true when the function calls are from different threads, but also when the function is re-entered from the same thread which can occur in presence of recursion, or asynchronous signals.

While the thread is sleeping, another thread rolls in and replaces x with a different value, and the two threads return the latter's value + 1 and + 2 respectively. Is this possible?

This isn't possible, because each x is a distinct object.

when and how is memory separate in c++ multithreading?

The entire memory space is shared across all threads.

But whether a variable names the same object is a different matter. Automatic variables are distinct each time their scope is entered as I mentioned earlier. Thread local variables name a distinct object in each thread. Static variables name the same object in all threads and are very susceptible to multi-threading bugs.

eerorika
  • 232,697
  • 12
  • 197
  • 326