Solved

reentry of functions through different threads

Posted on 1998-11-24
7
223 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
Industry Leaders: 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: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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 104
maze travler 6 66
White board coding practice 3 93
How do I get Window Title of all opened process? 4 35
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…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 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