Solved

thread problem

Posted on 1998-08-14
7
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

Technology Partners: 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!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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