Link to home
Start Free TrialLog in
Avatar of webrage
webrage

asked on

thread problem

this is the problem...
i start three threads with the _beginthreadex function
when sending integers values to thread function it works
perfect. But i need to send strings(char *) when i do that
the parameters tend to be the same in all three threads.
wtf is wrong?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

you are probably "reusing" the string buffer (that memory stores the string) for each case, right?  Like

char Str[100];

strcpy(Str,"Thread A's message.");
BeginThreadA(Str);
strcpy(Str,"Thread B's message.");
BeginThreadB(B);
strcpy(Str,"Thread C's message.");
BeginThreadC(C);

Something like that?
What is happening is that the new thread doesn't start right away or doesn't use its parameter (the pointer to the string) right away.  Then you go and alter the string for the next thread.  The the new thread uses the string and finds the new value, not the old one.

Make sense?

What to do about this.  That depends.  One approach would be to dynamically allocate (new) strings to be passed to each thread.  Then each thread would destroy (delete) each string when it was no longer needed.  (The code that allocates the string cannot delete it.  That would be another case of the same problem.)  There may be a better (easier) solution, depending on your circumstances, but that will work.

Let me know if you have questions.
Avatar of webrage

ASKER



yes maybe i have created a linked list..
the linked list have a char *directory  
but i declare memory for all elements.
i also have startthread function in the linked list

startthread(){
 printf(directory); //here it prints the right value..
 beginthread(directory,thrfunc);
}

thrfunc((void *)ch){
 printf("%s",(char *)ch); //here it doesnt..
}
Avatar of webrage

ASKER

oh great it now works
I did not understand your comment about the linked list, etc.   Do I need to or is everything okay now?  If you need more help, try to explain that again.
Avatar of webrage

ASKER

no it works perfectly now.. I didnt planned to send that message!Sorry..