Sponsored Content
Full Discussion: scope
Top Forums Programming scope Post 302077633 by jim mcnamara on Friday 23rd of June 2006 03:31:18 PM
Old 06-23-2006
Okay. Let me try again. First, static variables and multithreading do not make good neighbors.

static variables for a function are allocated ONCE, not on the stack like automatic variables, but in one single separate area called the BSS. That means every thread accesses the same memory location for a static variable.

automatic variables exist as separate copies for each invocation (thread) of the function. So there are mutltiple copes of those variables with function scope only. That means threads can't stomp on each other's data and trash what your code is trying to do, which is what happens with static varaibles.


Read this link and maybe you will see what I'm trying to say:
http://wearcam.org/ece385/lecture5/cexamples/bss.htm
 

10 More Discussions You Might Find Interesting

1. Programming

C++: scope, different files etc..

I'm having a problem getting this to work.. I got 3 files, start.C - Where i got my main() function Menu.C & Menu.h - Where I'm trying to use hash_map start.C #include <iostream> #include "Menu.h" using namespace std; int main() { /* test code here */ return 0; } Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies

2. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

3. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

4. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

5. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

6. AIX

`pthread_rwlock_t' was not declared in this scope

Hello All, I am getting this error while compiling my application on IBM AIX 5.3. As I tried to define _XOPEN_SOURCE=500 in makefile, that didn't work. Please help us to resolve the error. (0 Replies)
Discussion started by: mustus
0 Replies

7. Shell Programming and Scripting

Scope of variables between scripts

Friends, I am using ksh under SunoS. This is what I have In file1.sh NOW=$(date +"%b-%d-%y") LOGFILE="./log-$NOW.log" I will be using this file through file1.sh as log file. I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies

8. Shell Programming and Scripting

bc: scope doesn't work for me

I am trying to use bc to calculate the difference between two nano second time stamps. bc does the calculation but seems to ignore the scale option: micro_start=$(date +%s.%N) # .. some stuff happens here micro_stop=$(date +%s.%N) TOT=$(echo "scale=3; $micro_stop - $micro_start" | bc)... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

9. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

10. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies
PTHREAD_SPECIFIC(3)					     Library Functions Manual					       PTHREAD_SPECIFIC(3)

NAME
pthread_key_create, pthread_key_delete, pthread_setspecific, pthread_getspecific - management of thread-specific data SYNOPSIS
#include <pthread.h> int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); int pthread_key_delete(pthread_key_t key); int pthread_setspecific(pthread_key_t key, const void *pointer); void * pthread_getspecific(pthread_key_t key); DESCRIPTION
Programs often need global or static variables that have different values in different threads. Since threads share one memory space, this cannot be achieved with regular variables. Thread-specific data is the POSIX threads answer to this need. Each thread possesses a private memory block, the thread-specific data area, or TSD area for short. This area is indexed by TSD keys. The TSD area associates values of type void * to TSD keys. TSD keys are common to all threads, but the value associated with a given TSD key can be different in each thread. For concreteness, the TSD areas can be viewed as arrays of void * pointers, TSD keys as integer indices into these arrays, and the value of a TSD key as the value of the corresponding array element in the calling thread. When a thread is created, its TSD area initially associates NULL with all keys. pthread_key_create allocates a new TSD key. The key is stored in the location pointed to by key. There is a limit of PTHREAD_KEYS_MAX on the number of keys allocated at a given time. The value initially associated with the returned key is NULL in all currently executing threads. The destr_function argument, if not NULL, specifies a destructor function associated with the key. When a thread terminates via pthread_exit or by cancellation, destr_function is called with arguments the value associated with the key in that thread. The destr_func- tion is not called if that value is NULL. The order in which destructor functions are called at thread termination time is unspecified. Before the destructor function is called, the NULL value is associated with the key in the current thread. A destructor function might, however, re-associate non-NULL values to that key or some other key. To deal with this, if after all the destructors have been called for all non-NULL values, there are still some non-NULL values with associated destructors, then the process is repeated. The LinuxThreads implementation stops the process after PTHREAD_DESTRUCTOR_ITERATIONS iterations, even if some non-NULL values with associated descriptors remain. Other implementations may loop indefinitely. pthread_key_delete deallocates a TSD key. It does not check whether non-NULL values are associated with that key in the currently executing threads, nor call the destructor function associated with the key. pthread_setspecific changes the value associated with key in the calling thread, storing the given pointer instead. pthread_getspecific returns the value currently associated with key in the calling thread. RETURN VALUE
pthread_key_create, pthread_key_delete, and pthread_setspecific return 0 on success and a non-zero error code on failure. If successful, pthread_key_create stores the newly allocated key in the location pointed to by its key argument. pthread_getspecific returns the value associated with key on success, and NULL on error. ERRORS
pthread_key_create returns the following error code on error: EAGAIN PTHREAD_KEYS_MAX keys are already allocated pthread_key_delete and pthread_setspecific return the following error code on error: EINVAL key is not a valid, allocated TSD key pthread_getspecific returns NULL if key is not a valid, allocated TSD key. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_create(3), pthread_exit(3), pthread_testcancel(3). EXAMPLE
The following code fragment allocates a thread-specific array of 100 characters, with automatic reclaimation at thread exit: /* Key for the thread-specific buffer */ static pthread_key_t buffer_key; /* Once-only initialisation of the key */ static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT; /* Allocate the thread-specific buffer */ void buffer_alloc(void) { pthread_once(&buffer_key_once, buffer_key_alloc); pthread_setspecific(buffer_key, malloc(100)); } /* Return the thread-specific buffer */ char * get_buffer(void) { return (char *) pthread_getspecific(buffer_key); } /* Allocate the key */ static void buffer_key_alloc() { pthread_key_create(&buffer_key, buffer_destroy); } /* Free the thread-specific buffer */ static void buffer_destroy(void * buf) { free(buf); } LinuxThreads PTHREAD_SPECIFIC(3)
All times are GMT -4. The time now is 11:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy