Solved

reentry of functions through different threads

Posted on 1998-11-24
7
217 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 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.

776 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