Solved

reentry of functions through different threads

Posted on 1998-11-24
7
212 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

12 Experts available now in Live!

Get 1:1 Help Now