Re: CAsyncsocket derived class being run as a thread
- From: Ian Semmel <isemmelNOJUNK@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jan 2006 03:52:01 +1000
Well, it should be
if ( CreateThread () ) // everything created ok PostThreadMessage ...
I do it this way, because in the thread model I use, when I receive the message I do things like post messages back to the mainline that everything is OK (I mainly use threads for stream I/O)
I sometimes use this -> to pop up intelliwhatever list to get the function. Does no harm.
Joseph M. Newcomer wrote:
There are a couple issues here.
First, the thread can be created suspended.
Second, PostThreadMessage may or may not work correctly. For PostThreadMessage to work,
there must be a message queue in the receiving thread.
Microsoft's distinction of "UI threads" and "worker threads" is crap, and very confusing. There is only one kind of thread: a thread. There are two variants, threads with message pumps and threads without message pumps, erroneously called "UI threads" and "worker threads". Now, how does a thread become a "worker thread"? It is just a thread that has a message pump. What does a message pump do? It retrieves messages from the thread's message queue and dispatches them. How does the message queue get created? When an operation like GetMessage, PeekMessage, etc. is executed. When is it executed? Who knows? So the race condition exists in your code below; when the PostThreadMessage is executed, the thread may or may not have a message queue into which the message can be queued.
The solution is to do this asynchronously. For example, when I need to do this, I will do a PeekMessage call with PM_NOREMOVE in the thread's InitInstance handler (this is one of hte exceptions to "PeekMessage is evil"), which creates the message queue. Then I will PostMessage back to the UI thread a notification that the thread queue now exists, and in response to that message, I will PostThreadMessage the start request.
Note that this-> is redundant. joe
On Mon, 09 Jan 2006 07:27:51 +1000, Ian Semmel <isemmelNOJUNK@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
You can call anything, but what you have got to remember is after CreateThread () is completed, the code is executing in a new context, not that when you called CreateThread, so all the normal caveats apply.
What I do is start the thread from a function like
void CMyThread::StartThreading () { CreateThread (); this -> PostThreadMessage ( UTM_START_THREAD, 0, 0 ); }
When I receive this message (ON_THREAD_MESSAGE), I know that the thread is running.
Paul Lee wrote:
Ian Semmel wrote:
You don't have to use AfxBeginThread to start your thread
You can do something like
UDPListener* pThread = new UDPListener ();
pThread->SetReceivePort( iPort);
pThread -> CreateThread (); // this could go in your Start() function
Thanks!
I'm at a loss as to how to proceed now, with all the good
information provided. Just a little question:
after calling CreateThread(), can I then call any of the other member functions of UDPListener, such as
pThread->Expunge();
Thanks a lot!
Paul
Joseph M. Newcomer [MVP] email: newcomer@xxxxxxxxxxxx Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: CAsyncsocket derived class being run as a thread
- From: Joseph M . Newcomer
- Re: CAsyncsocket derived class being run as a thread
- References:
- CAsyncsocket derived class being run as a thread
- From: paul
- Re: CAsyncsocket derived class being run as a thread
- From: Joseph M . Newcomer
- Re: CAsyncsocket derived class being run as a thread
- From: paul
- Re: CAsyncsocket derived class being run as a thread
- From: Joseph M . Newcomer
- Re: CAsyncsocket derived class being run as a thread
- From: paul
- Re: CAsyncsocket derived class being run as a thread
- From: Ian Semmel
- Re: CAsyncsocket derived class being run as a thread
- From: Paul Lee
- Re: CAsyncsocket derived class being run as a thread
- From: Ian Semmel
- Re: CAsyncsocket derived class being run as a thread
- From: Joseph M . Newcomer
- CAsyncsocket derived class being run as a thread
- Prev by Date: Re: Preprocessor definitions in Release Mode
- Next by Date: Re: Problem due to Worker Threads?
- Previous by thread: Re: CAsyncsocket derived class being run as a thread
- Next by thread: Re: CAsyncsocket derived class being run as a thread
- Index(es):
Relevant Pages
|