problem in Passing arguements to pthread_create


 
Thread Tools Search this Thread
Operating Systems Linux problem in Passing arguements to pthread_create
# 1  
Old 05-15-2009
problem in Passing arguements to pthread_create

Hello every one

My question may look very easy, sorry for my ignorance.
I am not able to figure the problem.

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void *myfunc(void *vptr_value) {
int value = *((int *)vptr_value);
printf("Thread value: %d \n", value);
return NULL;
}
int main() {
pthread_t tid[2];
int i ;
for(i=0; i<2;i++)
pthread_create(&tid[i], NULL, myfunc, &i);

for(i=0;i<2;i++)
pthread_join(tid[i], NULL);
return 0;
}

I wrote a simple program to pass a argument to a thread work function.
I am passing address of i, i wanted to print the value of i.
But its printing same value for both the threads.

Can any one help me out in this????

Thankyou......
# 2  
Old 05-15-2009
WorkAround

Pls add a sleep statement ... i.e.
sleep 1;
after pthread_create()
And you will get what is expected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Too many arguements error in SH script

Getting this error: ./check_db_locks.sh SAMPLE ./check_db_locks.sh: line 39: if ; then lockids=`db2 get snapshot for locks on $1 |grep "ID of agent holding lock" |awk -F"= " '{print $2}' |sort -u` echo "$STATE_CRITICAL&DB2 - $locks lock(s). Possible app. handles are:... (2 Replies)
Discussion started by: Frank Velasco
2 Replies

2. Programming

fork vs pthread_create

Suppose I have a simple program main() with a global varibale int x=0. int x = 0; main() { print("%d\n",x); } I want to create two threads/process which must access this variable x in sync. Which one will be better threads( pthread_create ) or process( fork )? If I go with fork() then... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

3. UNIX for Dummies Questions & Answers

Pthread_create problem

Hi, I'm trying to do my homework assignment but I am having trouble using the pthread_create fucntion. Here is my code________________ //Alicia Johnson //sum_pid program //creates n number of threads. These threads create a random number //then adds the number to a global array. Then... (1 Reply)
Discussion started by: ajohns38
1 Replies

4. Programming

pthread_create

The prototype for pthread_create function is like this:- int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start routine),void *arg); Q.1 .Why the return type of the start_routine must be void*?? Q.2. Why should we pass arg by converting into void * only ?? Thank You (3 Replies)
Discussion started by: sunil_abhay
3 Replies

5. Programming

How Can I use pthread_create ?

Hi. I use C++ and I wishes to create a thread with the pthread_create function, my question is, how can I do this if I wish that the function will be a member of the class ?? I know from windows programming that I can declare a static function like this static unsigned int __stdcall... (7 Replies)
Discussion started by: shvalb
7 Replies

6. Programming

Pthread_create issue

Hello My problem goes like this: I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code. Any help will be highly appreciated.... (1 Reply)
Discussion started by: Hellboy
1 Replies

7. Shell Programming and Scripting

pass input arguements to DB2 SQL script

Hi all, I have a shell script which invoke a sql script using command db2 -tf /home/me/db_housekeep.sql -z /home/me/db_housekeep.log however, this mentioned sql script requires several input arguments, I wonder if one can pass variables from shell script to sql script? thanks! (0 Replies)
Discussion started by: mpang_
0 Replies

8. Programming

unresolve pthread_create etc

how to do with that? after cc -o xxxx xxxx.c ld: Unresolved: _pthread_create _pthread_deteach _pthread_exit Thanks (3 Replies)
Discussion started by: zhshqzyc
3 Replies

9. Programming

pthread_create problem

Here is simple code for multithreading in POSIX: void* simplethread(void* arg) { printf("Hello World\n"); } int main(void) { pthread_t id; pthread_create(&id, NULL, simplethread, NULL); return 0; } Whether the new thread will run or not depends on the OS. Tricky ... (5 Replies)
Discussion started by: _rocky
5 Replies

10. UNIX for Dummies Questions & Answers

find files and using them as input arguements for another command

I need to do the following: 1) find files in certain directories that have todays date stamp 2) use these files as input arguements into another command (1 Reply)
Discussion started by: bobbygrep
1 Replies
Login or Register to Ask a Question