Solved

reentry of functions through different threads

Posted on 1998-11-24
7
214 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
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: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

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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.

911 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

25 Experts available now in Live!

Get 1:1 Help Now