Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


thread problem

Posted on 1998-08-14
Medium Priority
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
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
LVL 22

Accepted Solution

nietod earned 200 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.
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.


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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

721 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