Sponsored Content
Top Forums Programming Creating an array to hold posix thread ids: Only dynamic array works Post 302238443 by kmehta on Saturday 20th of September 2008 03:57:09 AM
Old 09-20-2008
Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads:
Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output.

Snippet 1
This works:
--------------
int *threadids;
threadids = (int *) malloc (num_threads * sizeof(int));

for(i = 0; i<num_threads; i++)
threadids[i] = i;

for(i=0; i<num_threads; i++)
pthread_create(&threads[i], NULL, pthread_init, &threadids[i]);

.
.
void *pthread_init(void *threadid)
{
int tid;
tid = (int)(*(int *)threadid));
printf("Thread id is %d", tid);
pthread_exit(NULL);
}

Output: Thread id is 0
--------------

Snippet 2
This doesn't work:
--------------
int threadids[num_threads];
for(i = 0; i<num_threads; i++)
threadids[i] = i;
.
.
.
Output: Thread id is 634800
--------------

As you can see, the only difference is in the way the array threadids is declared. But the second snippet gives a garbage value.

I will be glad if someone points out whats going wrong with the first snippet. Thanks.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a dynamic array in ksh

Hi, Is it possible to create a dynamic array in shell script. I am trying to get the list of logfiles that created that day and put it in a dynamic array. I am not sure about it. help me New to scripting Gundu (3 Replies)
Discussion started by: gundu
3 Replies

2. UNIX for Advanced & Expert Users

MAX SIZE ARRAY Can Hold it

Hi, Do anyone know what's the max size of array (in awk) can be store before hit any memory issue. Regards (3 Replies)
Discussion started by: epall
3 Replies

3. Shell Programming and Scripting

Dynamic Array Issue

Could one of you, please, provide some input regarding my problem below and it is as follows: I have 2 files that I need to make sure are identical before processing: First, I sort both files Second, I do a diff file1 file2 > File 3 This provides me with the difference. Now, I need to... (6 Replies)
Discussion started by: ddedic
6 Replies

4. Shell Programming and Scripting

How to hold string array in shell scripts

Gents, Below is the Shell script which I am trying to hold a string of array that is passed from a java file. But it is not working . Can any one please help me to by fixing it. #!/bin/csh/ set copy = ($argv) echo $copy >> /home/users/bavananr/rrr.log echo $copy >>... (3 Replies)
Discussion started by: brajesh
3 Replies

5. Shell Programming and Scripting

creating a dynamic array

i want to create an array the array elements are populated depending upon the number of entries present in a data file The data file is created dynamically how to achieve the same thanks (1 Reply)
Discussion started by: trichyselva
1 Replies

6. Programming

array dynamic allocation

Hi, I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Discussion started by: littleboyblu
11 Replies

7. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

8. Programming

readdir and dynamic array memory corruption

Hi everyone I am developing an utility. At some part of it I read directory entries to a dynamic array: struct list It stores pointers to items: list.entries, which are structures: struct entry If a number of files in a directory is greater then number of elements an array was initially... (11 Replies)
Discussion started by: torbium
11 Replies

9. Programming

Memory corruption in dynamic array of strings

I put together a C function to add strings to a dynamic array of strings (mostly for educational purpose to explain pointers to my kid). It works, but sometimes one or two strings in the array becomes corrupted. Running example on 64 bit Ubuntu, gcc ver. 4.8.4 Hope my code is self-explanatory: ... (2 Replies)
Discussion started by: migurus
2 Replies

10. Shell Programming and Scripting

Creating a pseudo-array in dash, (POSIX).

Arrays in dash, (POSIX). Hi gurus... I am thinking of trying AudioScope.sh in pure POSIX so... I need an array in dash, I know it is not possible but pseudo-arrays are. I have two versions that work, the second is an idea from the WWW. The first is what I would like to use. There are... (8 Replies)
Discussion started by: wisecracker
8 Replies
pthread_key_create(3C)					   Standard C Library Functions 				    pthread_key_create(3C)

NAME
pthread_key_create, pthread_key_create_once_np - create thread-specific data key SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); int pthread_key_create_once_np(pthread_key_t *key, void (*destructor)(void*)); DESCRIPTION
The pthread_key_create() function creates a thread-specific data key visible to all threads in the process. Key values provided by pthread_key_create() are opaque objects used to locate thread-specific data. Although the same key value may be used by different threads, the values bound to the key by pthread_setspecific() are maintained on a per-thread basis and persist for the life of the calling thread. Upon key creation, the value NULL is associated with the new key in all active threads. Upon thread creation, the value NULL is associ- ated with all defined keys in the new thread. An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with that key, the function pointed to is called with the current associated value as its sole argument. Destructors can be called in any order. If, after all the destructors have been called for all keys with non-NULL values, there are still some keys with non-NULL values, the process will be repeated. If, after at least PTHREAD_DESTRUCTOR_ITERATIONS iterations of destructor calls for outstanding non-NULL values, there are still some keys with non-NULL values, the process is continued, even though this might result in an infinite loop. An exiting thread runs with all signals blocked. All thread termination functions, including thread-specific data destructor functions, are called with all signals blocked. The pthread_key_create_once_np() function is identical to the pthread_key_create() function except that the key referred to by *key must be statically initialized with the value PTHREAD_ONCE_KEY_NP before calling pthread_key_create_once_np(), and the key is created exactly once. This function call is equivalent to using pthread_once(3C) to call a onetime initialization function that calls pthread_key_create() to create the data key. RETURN VALUES
If successful, the pthread_key_create() and pthread_key_create_once_np() functions store the newly created key value at *key and return 0. Otherwise, an error number is returned to indicate the error. ERRORS
The pthread_key_create() and pthread_key_create_once_np() functions will fail if: EAGAIN The system lacked the necessary resources to create another thread-specific data key, or the system-imposed limit on the total number of keys per process PTHREAD_KEYS_MAX has been exceeded. ENOMEM Insufficient memory exists to create the key. The pthread_key_create() and pthread_key_create_once_np() functions will not return an error value of EINTR. EXAMPLES
Example 1 Call thread-specific data in the function from more than one thread without special initialization. In the following example, the thread-specific data in the function can be called from more than one thread without special initialization. For each argument passed to the executable, a thread is created and privately bound to the string-value of that argument. /* cc -mt thisfile.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> static void *thread_function(void *); static void show_tsd(void); static void cleanup(void*); #define MAX_THREADS 20 static pthread_key_t tsd_key = PTHREAD_ONCE_KEY_NP; int main(int argc, char *argv[]) { pthread_t tid[MAX_THREADS]; int num_threads; int i; if ((num_threads = argc - 1) > MAX_THREADS) num_threads = MAX_THREADS; for (i = 0; i < num_threads; i++) pthread_create(&tid[i], NULL, thread_function, argv[i+1]); for (i = 0; i < num_threads; i++) pthread_join(tid[i], NULL); return(0); } static void * thread_function(void *arg) { char *data; pthread_key_create_once_np(&tsd_key, cleanup); data = malloc(strlen(arg) + 1); strcpy(data, arg); pthread_setspecific(tsd_key, data); show_tsd(); return (NULL); } static void show_tsd() { void *tsd = pthread_getspecific(tsd_key); printf("tsd for %d = %s ", pthread_self(), (char *)tsd); } /* application-specific clean-up function */ static void cleanup(void *tsd) { printf("freeing tsd for %d = %s ", pthread_self(), (char *)tsd); free(tsd); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed. | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ |Standard |See below. | +-----------------------------+-----------------------------+ For pthread_key_create(), see standards(5). SEE ALSO
pthread_once(3C), pthread_getspecific(3C), pthread_setspecific(3C), pthread_key_delete(3C), attributes(5), standards(5) SunOS 5.11 2 Nov 2007 pthread_key_create(3C)
All times are GMT -4. The time now is 09:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy