Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does local variable in thread function have separe copy according to thread?

I have declared some local variable in one function like this:

void* thread_function (void* parameter)
{
   struct parameter * thread_data = (struct parameter *)parameter;
   char buffer[20];
   int temp;
}

Here if I have created two threads then in one thread if buffer & temp is updated so will it effect other thread ?

i mean if there are two thread then does there will be two copy of all local variable?

EDIT : then in which case i need to used thread specific data.? i mean pthread_setspecific & all such stuff

like image 789
Jeegar Patel Avatar asked Sep 12 '11 12:09

Jeegar Patel


People also ask

Are local variables shared between threads?

Tip: Unlike class and instance field variables, threads cannot share local variables and parameters. The reason: Local variables and parameters allocate on a thread's method-call stack. As a result, each thread receives its own copy of those variables.

Are local variables shared between threads in C?

But: No, threads do not share real local variables.

How do thread local variables work?

The local variables of a function are unique to each thread that runs the function. However, the static and global variables are shared by all threads in the process. With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index.

Do all threads share 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.


3 Answers

It's not that each thread has its own copy, it's that each instance of a function invocation has its own copy of all automatic (i.e. local non-static) variables, regardless of whether the instances are in the same thread or different threads. This is true if the instances come into existence due to invocation in different threads, recursive invocation, mutual/indirect recursion, or even invocation from an asynchronous signal handler. Note that while the C standard does not specify threads, the relevant section in the standard is probably 5.2.3 Signals and interrupts:

Functions shall be implemented such that they may be interrupted at any time by a signal, or may be called by a signal handler, or both, with no alteration to earlier, but still active, invocations' control flow (after the interruption), function return values, or objects with automatic storage duration. All such objects shall be maintained outside the function image (the instructions that compose the executable representation of a function) on a per-invocation basis.

This makes it explicit that each invocation must have its own storage for automatic variables.

like image 70
R.. GitHub STOP HELPING ICE Avatar answered Oct 06 '22 00:10

R.. GitHub STOP HELPING ICE


These variables are allocated on the stack, and each thread has its own stack: these variables are private to each thread (they are not shared). (See this answer for more details.)

If you assign thread_data to a global pointer, for example, other threads will be able to access thread_data via the global pointer.

Thread specific data (e.g. pthread_setspecific) is used to create variables that are global, but still specific to each thread (not shared): They are thread-specific global variables.

You need to use thread specific variables when you want global variables, but don't want to share them between threads.

like image 25
Arnaud Le Blanc Avatar answered Oct 06 '22 01:10

Arnaud Le Blanc


Local variables are stored in stack memory, which is private to a thread.

Therefore they are not shared between threads: there will be an independent copy of each variable in each thread

Update Whether you would want to share data between threads really boils down to a design question; What are your threads doing? Are their effort co-ordinated or are they simply workers processing a queue.

The main thing to consider is synchronization of shared data. Variables that are shared between threads are variables that can change value unexpectedly (within a single thread) and so need to be treated as such. I would suggest that you err on the side of not sharing, unless you have a specific reason to do so.

like image 35
Dancrumb Avatar answered Oct 06 '22 01:10

Dancrumb