LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing int array to thread? (https://www.linuxquestions.org/questions/programming-9/passing-int-array-to-thread-365608/)

Thinking 09-21-2005 10:38 AM

passing int array to thread?
 
hiho@ll

how does this really work:
Code:

int array[2]={0};
array[0]=10;
array[1]=20;

pthread_create(&thread,NULL,this->mythread,(void*)array);

void* mythread(void *arg){
 int *tmp=(int*)arg;
 printf("OUTPUT: %d %d\n",tmp[0],tmp[1]);
}

i get:
OUTPUT: 134583088 1078320720

what's this??
i think it's a cast problem
but how do i pass a int array to a thread?

thx@ll

deiussum 09-21-2005 10:59 AM

I'm guessing your array is a local variable on the stack. Once you leave the function that you declared that variable in, the memory for it goes away, so you are probably using a dangling pointer in your thread function. Try allocating the memory for the array with new/malloc instead.

Thinking 09-21-2005 11:00 AM

edit: thx, i didn't see your post because i wrote this post beneath while you wrote your reply ;-)
i'll check it tomorrow
thx


i think the problem was/is that i want to code something like a proxy class

the proxy class generates two threads which are listening on 2 sockets using select();
because i want it really full duplex i use 2 threads

if i use a static char* buffer both threads get the socket descriptor numbers

but i can't really use a static buffer, not?
because if there is another object, the threads will overwrite themself, not?
hmm *thinking*


All times are GMT -5. The time now is 10:44 PM.