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
thr_keycreate(3C)														 thr_keycreate(3C)

NAME
thr_keycreate, thr_setspecific, thr_getspecific - thread-specific data functions SYNOPSIS
cc -mt [ flag... ] file...[ library... ] #include <thread.h> int thr_keycreate(thread_key_t *keyp, void (*destructor)(void *)); int thr_setspecific(thread_key_t key, void *value); int thr_getspecific(thread_key_t key, void **valuep); Create Key In general, thread key creation allocates a key that locates data specific to each thread in the process. The key is global to all threads in the process, which allows each thread to bind a value to the key once the key has been created. The key independently maintains specific values for each binding thread. The thr_keycreate() function allocates a global key namespace, pointed to by keyp, that is visible to all threads in the process. Each thread is initially bound to a private element of this key, which allows access to its thread-specific data. Upon key creation, a new key is assigned the value NULL for all active threads. Additionally, upon thread creation, all previously created keys in the new thread are assigned the value NULL. Optionally, a destructor function destructor can be associated with each key. Upon thread exit, if a key has a non-null destructor func- tion and the thread has a non-null value associated with that key, the destructor function is called with the current associated value. If more than one destructor exists for a thread when it exits, the order of destructor calls is unspecified. Set Value Once a key has been created, each thread can bind a new value to the key using thr_setspecific(). The values are unique to the binding thread and are individually maintained. These values continue for the life of the calling thread. Proper synchronization of key storage and access must be ensured by the caller. The value argument to thr_setspecific() is generally a pointer to a block of dynamically allocated memory reserved by the calling thread for its own use. See EXAMPLES below. At thread exit, the destructor function, which is associated at time of creation, is called and it uses the specific key value as its sole argument. Get Value thr_getspecific() stores the current value bound to key for the calling thread into the location pointed to by valuep. If successful, thr_keycreate(), thr_setspecific() and thr_getspecific() return 0. Otherwise, an error number is returned to indicate the error. If the following conditions occur, thr_keycreate() returns the corresponding error number: EAGAIN The system lacked the necessary resources to create another thread-specific data key. ENOMEM Insufficient memory exists to create the key. If the following conditions occur, thr_keycreate() and thr_setspecific() return the corresponding error number: ENOMEM Insufficient memory exists to associate the value with the key. The thr_setspecific() function returns the corresponding error number: EINVAL The key value is invalid. Example 1: Call the thread-specific data from more than one thread without special initialization. In this example, the thread-specific data in this 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 <thread.h> void *thread_specific_data(void *); void cleanup(void*); #define MAX_ARGC 20 thread_t tid[MAX_ARGC]; int num_threads; int main(int argc, char *argv[]) { int i; num_threads = argc - 1; for (i = 0; i < num_threads; i++) thr_create(NULL, 0, thread_specific_data, argv[i+1], 0, &tid[i]); for (i = 0; i < num_threads; i++) thr_join(tid[i], NULL, NULL); return(0); } /* end main */ void * thread_specific_data(void *arg) { static mutex_t keylock; /* static ensures only one copy of keylock */ static thread_key_t key; static int once_per_keyname = 0; char *private_data = arg; void *tsd = NULL; void *data; if (!once_per_keyname) { mutex_lock(&keylock); if (!once_per_keyname) { thr_keycreate(&key, cleanup); once_per_keyname++; } mutex_unlock(&keylock); } thr_getspecific(key, &tsd); if (tsd == NULL) { data = malloc(strlen(private_data) + 1); strcpy(data, private_data); thr_setspecific(key, data); thr_getspecific(key, &tsd); } printf("tsd for %d = %s ", thr_self(), (char *)tsd); thr_getspecific(key, &tsd); printf("tsd for %d remains %s ", thr_self(), (char *)tsd); return (NULL); } /* end thread_specific_data */ void cleanup(void *v) { /* application-specific clean-up function */ free(v); } See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Stable | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ thr_exit(3C), attributes(5), standards(5) WARNINGS
The thr_getspecific() and thr_getspecific() functions can be called either explicitly or implicitly from a thread-specific data destructor function. Calling thr_setspecific() from a destructor can result in lost storage or infinite loops. 1 Sep 2005 thr_keycreate(3C)
All times are GMT -4. The time now is 03:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy