thread problem

Posted on 1998-08-14
Last Modified: 2010-04-01
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?
Question by:webrage
  • 4
  • 3
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 1170281
answer coming.
LVL 22

Expert Comment

ID: 1170282
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.");
strcpy(Str,"Thread B's message.");
strcpy(Str,"Thread C's message.");

Something like that?
LVL 22

Expert Comment

ID: 1170283
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 1170284

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

 printf(directory); //here it prints the right value..

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

Author Comment

ID: 1170285
oh great it now works
LVL 22

Expert Comment

ID: 1170286
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.

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question