Thread function local variables


 
Thread Tools Search this Thread
Top Forums Programming Thread function local variables
# 1  
Old 03-20-2011
Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves?

like
Code:
thread_func()
{
   int i, j;
   string...

}

Are the above local variables defined for each of the threads? in that case if i call threads simultaneously, their local variables will not conflict
# 2  
Old 03-20-2011
Quote:
Originally Posted by saman_glorious
As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves?

like
Code:
thread_func()
{
   int i, j;
   string...

}

Are the above local variables defined for each of the threads? in that case if i call threads simultaneously, their local variables will not conflict
Every thread has its own stack. Hence, in your example, i and j would be hence "private to the threads" and thus their local variables "would not conflict".

Of course, if another thread happens to know the address of variable i (or j) of a thread, it may access/modify it. But that's not an usual pattern.

HTH, Loïc
This User Gave Thanks to Loic Domaigne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make nested function local?

Hi, If I declare a function inside another function, it overwrites any previously declared function with the same name. This is NOT what I want. Example: #!/bin/bash _test() { echo test; } _myf() { # I'm using the same name as the other function. _test() { echo local test; }... (8 Replies)
Discussion started by: chebarbudo
8 Replies

2. Shell Programming and Scripting

Listing all local variables for unset

I have tried to thoroughly search other threads before posting this question... I have a shell script (bsh) that I'd like to "re-execute" if the user chooses to. Before the program is executed again the local variables (those set within the script) need to be unset. Is there a command that... (6 Replies)
Discussion started by: powwm
6 Replies

3. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

4. Programming

Thread local storage on ancient AIX

What I'm given: Pure C code in one file that has to compile on Windows, SunOS, and AIX. My task is making minimal changes to the code to make it thread safe. I'm not in control of threads - they are created elsewhere. The code has a function invoked externally. The first call to this function is... (1 Reply)
Discussion started by: vkleban
1 Replies

5. Shell Programming and Scripting

'while' loop does not change local variables?!

(I think this question desearves separate thread..) I have a problem with 'while' I am trying to set variables by 'while' and it is fine inside, but after completting the loop all changes are lost: > bb="kkkk - 111\nlllll - 22222\nbbbb - 4444" > echo "$bb" kkkk - 111 lllll - 22222 bbbb -... (3 Replies)
Discussion started by: alex_5161
3 Replies

6. UNIX for Dummies Questions & Answers

How to call a local function within Awk

Hi, I have the following statement which parses a string for me and prints it out: l_comp="dc000.runksh.test.ksh| $g_sql/dc0000.runksh_test.sql|new.dat|control.ctl" echo $l_comp | awk -F"|" '{ for ( i = 1; i <= NF; i++) { print $i; } } ' Rather then printing the data, I would like to... (5 Replies)
Discussion started by: CAGIRL
5 Replies

7. Shell Programming and Scripting

How to execute local function in awk

Hi All, Can you please tell me how to execute local function written in a shell script with awk. i tried with system command but its giving an error. (1 Reply)
Discussion started by: krishna_gnv
1 Replies

8. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies

9. Programming

Can't dlopen() a library containing Thread Local Storage

Hi, I have a small test c program which tries to dlopen a shared library(libjvm.sl). But i get error as "Can't dlopen() a library containing Thread Local Storage" My program is as below when i run the program i get error any pointers why the error?? I am using hp-ux . The... (1 Reply)
Discussion started by: shriashishpatil
1 Replies

10. UNIX for Dummies Questions & Answers

rsh with local variables

Hi, I am trying to do an rsh and execute the same script on a distant unix computer. The problem is that I need to get all the local variables of the distant computer to launch correctly my script. I'm working on AIX 4.3.3 I try to execute .profile in the rsh but it seems not to be... (1 Reply)
Discussion started by: jo_aze
1 Replies
Login or Register to Ask a Question