Solved

thread problem

Posted on 1998-08-14
7
228 Views
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?
0
Comment
Question by:webrage
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

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

Expert Comment

by:nietod
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.");
BeginThreadA(Str);
strcpy(Str,"Thread B's message.");
BeginThreadB(B);
strcpy(Str,"Thread C's message.");
BeginThreadC(C);

Something like that?
0
 
LVL 22

Expert Comment

by:nietod
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:webrage
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

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

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

Author Comment

by:webrage
ID: 1170285
oh great it now works
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now