Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do threads share local variables?

Tags:

I'm reading Operating System Concepts by Silberschatz 7th ed, and it says that threads of the same process share the code section, data section, and other O.S. resources, but have separate sets of stacks and registers. However, the problem set I'm working on states that threads share local variables, but aren't local variables stored on the stack, so individual threads should have their own copies?

like image 347
Tony Tarng Avatar asked Jan 13 '17 10:01

Tony Tarng


People also ask

Do threads share the same global variables?

Because threads within a process share the same memory map and hence share all global data (static variables, global variables, and memory that is dynamically-allocated via malloc or new), mutual exclusion is a critical part of application design.

Do threads share data?

Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.

What do threads share with each other?

Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.


2 Answers

Threads usually share the following.

  1. Data Segment(global variables,static data)
  2. Address space.
  3. Code Segment.
  4. I/O, if file is open , all threads can read/write to it.
  5. Process id of parent.
  6. The Heap

But threads maintain their own copy of stack,and local variables are stored on the stack so yeah you are right that each thread should have its own copy of local variables.

May be its a bad terminology used or may be its something specific to problem set.

like image 195
Sumeet Avatar answered Oct 12 '22 22:10

Sumeet


POSIX compliant OSs' threads have to share local variables (a.k.a. automatic variables). From Volume XBD, Base Definitions, Chapter 3, Definitions, Entry 3.404, Thread of the Open Group Base Specifications Issue 7 (at the time of answering):

Anything whose address may be determined by a thread, including but not limited to static variables, storage obtained via malloc(), directly addressable storage obtained through implementation-defined functions, and automatic variables, are accessible to all threads in the same process.

The following code, with output 10, should suffice for exemplifying my claim, if one assumes that static variables are indeed shared between threads:

#include <pthread.h> #include <unistd.h> #include <stdio.h>  static int* shared_pointer = NULL;  void* alter_thread_entry(void* arg) {     // The following variable will be reachable,     // while it exists, by both the main and the alter thread.     int local_variable = 10;     shared_pointer = &local_variable;     sleep(2);     return 0; }  int main() {     pthread_t alter_thread;     pthread_create(&alter_thread, NULL, alter_thread_entry, NULL);     sleep(1);     printf("%i", *shared_pointer);     fflush(stdout);     pthread_join(alter_thread, NULL); } 

The call stack that traces control execution of threads is what is not shared among threads of a process. However, this point is implementation-dependent, as nothing is established about it in the aforementioned standard.

like image 42
Jonathan Ginsburg Avatar answered Oct 12 '22 23:10

Jonathan Ginsburg