Thread Argument Passing


 
Thread Tools Search this Thread
Top Forums Programming Thread Argument Passing
# 1  
Old 01-19-2006
Thread Argument Passing

#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4

/* function to be executed by the new thread*/

void *PrintHello(void * threadid)
{
printf("\n %3d:Hello World!\n",threadid);
pthread_exit(NULL);
}


int main(int argc, char * argv[])
{
int *taskids[NUM_THREADS];
int rc,t;
pthread_t threads[NUM_THREADS];


for (t = 0; t < NUM_THREADS;t++)
{
taskids[t] = (int *)malloc(sizeof(int));
*taskids[t]=t;
printf("Creating thread %d\n",t);
printf(" The number in array is %d\n",*taskids[t]);
rc = pthread_create(&threads[t],NULL,PrintHello,(void *)&taskids[t]);
if (rc){
printf("ERROR;return code from pthread_create() is %d\n",rc);
exit(-1);
}
}

pthread_exit(NULL);
}


The above code is taken form the lecture notes at www.llnl.gov/computing/tutorials/pthreads under the topic passing arguments. When i run it on Sun SOlaris 9, the answe is wrong. Here is the answer: I cant detect the logic error.. PLease help.

Creating thread 0
The number in array is 0
Creating thread 1
The number in array is 1

-4195784:Hello World!
Creating thread 2
The number in array is 2
Creating thread 3
The number in array is 3

-4195780:Hello World!

-4195776:Hello World!

-4195772:Hello World!
# 2  
Old 01-19-2006
Those numbers [-4195772] are addresses displayed as an integer. You passed a pointer to HelloWorld, a pointer is an address.

try something like this:
Code:
printf("\n %3d:Hello World!\n",*(int *)threadid);

# 3  
Old 01-19-2006
or
printf("\n %p:Hello World!\n",threadid);

the %p can be used to print pointer address on most unix c compilers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

3. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

4. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

5. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

6. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

7. Programming

Passing argument to command in C

Hello all, New to C and I'm trying to write a program which can run a unix command. Would like to have the option of giving the user the ability to enter arguments e.g for "ls" be able to run "ls -l". I would appreciate any help. Thanks #include <stdio.h> #include <unistd.h> #include... (3 Replies)
Discussion started by: effizy
3 Replies

8. Shell Programming and Scripting

Using GET, passing argument to bash

Hi guys! So, I use GET ( Simple user agent using LWP library. ) on a remote text file that is then passed to bash and executed. However, I need to pass that bash script a single argument, and so far nothing that I have tried has worked, although I have learned quite a bit about input/output... (5 Replies)
Discussion started by: Rhije
5 Replies

9. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

10. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question