Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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?
0
webrage
Asked:
webrage
  • 4
  • 3
1 Solution
 
nietodCommented:
answer coming.
0
 
nietodCommented:
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?
0
 
nietodCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
webrageAuthor Commented:


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..
}
0
 
webrageAuthor Commented:
oh great it now works
0
 
nietodCommented:
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.
0
 
webrageAuthor Commented:
no it works perfectly now.. I didnt planned to send that message!Sorry..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now