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

x
?
Solved

reentry of functions through different threads

Posted on 1998-11-24
7
Medium Priority
?
227 Views
Last Modified: 2010-04-01
I am reentring a function which creates a thread (inside the thread I am initializing & dealing with socket connection). As I said, this function is called a few times and is supposed to create a new thread each time.
When debbuging, I see that although I pass the lines in which the creating function is called, only the first thread is called (due to the first call to the function),
but no other threads are created.
Are there any restrictions on reentring functions?
Any restrictions on creating multiple threads that start with the same function?
Please give an example.
Thanks,
     Gilbert Chang
0
Comment
Question by:gilbert_chang
[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

Expert Comment

by:nietod
ID: 1178597
How do you know no other threads are created?  How are you creating the threads?  Are you checking for error codes?  Do your realize that although a new thread may be created, it may not run right away.
0
 

Author Comment

by:gilbert_chang
ID: 1178598
I can view what threads are running while debugging using the icon that shows the debuggee's thread attributes in the VC++5.
I am creating threads using 'CreateThread'.
When looking for error codes, it seems like the threads are being created, but they dissapear quickly without doing their job...(running the function they should be started with)
If A thread does not run right away, how long can it take it? I am waiting for quite long periods of time (minutes!!).
Thanks.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178599
They may take on the order of a hundred milliseconds or so.  For some code designs (bad ones) that can be a problem.  Can you post the code you use to start the thread and the code of the started thread?
0
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

by:gilbert_chang
ID: 1178600
In the InitInstance of my CApp (was created by the MFC wizard):
mAppPtr = new App;
mAppPtr->StartIt();
mAppPtr->StartIt();
mAppPtr->StartIt();

then:

void App::StartIt()
{
HANDLE threadHandle;
DWORD threadID;

threadHandle = CreateThread (NULL ,0 ,Routine , this , 0 , &threadID);
}

Status _stdcall App::Routine( void * pvoid)
{
App * pseudo_this = (App *) pvoid;
pseudo_this->InitConnection();
DWORD myExitCode;
ExitThread(myExitCode );
return TRUE;
}

App::InitConnection()

{
//
// Here I create a socket + neccesary definitions, bind and try to connect
//
}

Thanks.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1178601
To begin with, one problem is that you cannot (should not) use CreateThread() from a C++ program.  A multi-threaded C++ program has a special version of the run-time library that is multi-thread compatible and provides procedures for startig a thread like beginthread().  These procedures use CreateThread(), to create the thread but do additional tasks, like initializing the  run-time library's information for the new thread.  However, in MFC, you shouldn't be using even those functions.  MFC provides threads features (that use beginthread) that should be used because they initialize MFC's information for the new thread.  You should be using a cWinThread object.  Look at cWinThread in the docs and then ask if you have questions.  I think things will work better when you switch to the cWinThread.  If not, let me know.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178602
So is this working for you?  Need more help?
0
 

Author Comment

by:gilbert_chang
ID: 1178603
Thanks a lot!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
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…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

715 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