Re: CAsyncsocket derived class being run as a thread

Tech-Archive recommends: Speed Up your PC by fixing your registry



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
.



Relevant Pages

  • Re: CAsyncsocket derived class being run as a thread
    ... PostThreadMessage may or may not work correctly. ... there must be a message queue in the receiving thread. ... What does a message pump do? ... >is completed, the code is executing in a new context, not that when you ...
    (microsoft.public.vc.mfc)
  • Re: CAsyncsocket derived class being run as a thread
    ... has a message queue, and it won't have a message queue until it issues a GetMessage, ... PostThreadMessage ... >>>is completed, the code is executing in a new context, not that when you ... Joseph M. Newcomer [MVP] ...
    (microsoft.public.vc.mfc)
  • Re: User thread problem
    ... >functionality using one thread instead of two. ... Did you read the documentation I posted which talked about PostThreadMessage ... failing if the target thread doesn't have a message queue? ...
    (microsoft.public.vc.mfc)
  • Re: Multithreading problem: losing message ptr after a PostThreadMessage() to a UI thread
    ... get created on the first call to PostThreadMessage which returns true. ... > The thread is almost certainly not yet running, has no message queue, so the ... > PostThreadMessage is guaranteed to fail ... > and it won't guarantee that the thread is running. ...
    (microsoft.public.vc.mfc)
  • Re: Need help solving a threading issue
    ... danger to PostThreadMessage. ... The circumstance in which PostThreadMessage ... is documented to fail involves an operating-system messaage pump (such as ... message pumps. ...
    (microsoft.public.vc.mfc)