Board index » Visual Studio » CWinThread::CreateThread

CWinThread::CreateThread

Visual Studio179
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both, how

is this distinguished?



2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)



Thanks in advance.


-
 

Re:CWinThread::CreateThread

See below...

On Mon, 03 Mar 2008 20:57:49 GMT, no.address@sorry.com.zzz (Tom Sherren) wrote:



Quote
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both, how

is this distinguished?

****

It starts a UI thread.

****

Quote


2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)

****

The CWinThread class is invoked. This means that the standard message pump is invoked.

So, for example, virtual CWinThread::InitInstance is called, so you would override this

and put code here to initialize your thread. Then upon completion virtual

CWinThread::ExitInstance is called, so you would override this to put code here to clean

up. The rest of the time, CWinThread::Run would be running, and you would typically not

override this; you would typically let the message pump to dispatch window and thread

messages (it is not uncommon to create invisible top-level windows in a secondary thread

to use them as message sinks; this is how SAPI and CAsyncSocket work, so you need UI

threads for those purposes, at least).



If you want the equivalent of a "worker thread" created by CWinThread::CreateThread, you

typically put your thread loop in the InitInstance handler and return FALSE when the loop

completes.

joe

****

Quote


Thanks in advance.

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:CWinThread::CreateThread

1. Technically you should use AfxBeginThread to create and start any thread.

But if under some special circumstance you want to keep your thread object

around and start it multiple times then I guess you can use CreateThread,

which Creates a UI thread only.



2. The starting point of a UI thread is that InitInstance method.



AliR.





"Tom Sherren" <no.address@sorry.com.zzz>wrote in message

Quote
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both,

how

is this distinguished?



2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)



Thanks in advance.





-

Re:CWinThread::CreateThread

"Tom Sherren" <no.address@sorry.com.zzz>wrote in message

Quote
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both,

how

is this distinguished?



Initially, a "worker" thread. If you start a message loop in the thread it

will become a "UI" thread.



Quote


2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)



The thread proc address is set by initilizing the

CWinThread::m_pfnThreadProc member variable.

CWinThread::m_pThreadParams sets the user data passed to the thread proc,

and CWinThread::::m_bAutoDelete controls auto-deletion of the CWinThread

object when the thread terminates.



Mark



--

Mark Salsbery

Microsoft MVP - Visual C++



Quote


Thanks in advance.



-

Re:CWinThread::CreateThread





"Tom Sherren" <no.address@sorry.com.zzz>wrote in message

Quote
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both,

how

is this distinguished?





I take that back - it starts a UI thread by default, but if you set the

object's m_pfnThreadProc member before calling CreateThread() on the object,

it will be a worker thread.



Mark



--

Mark Salsbery

Microsoft MVP - Visual C++



Quote


2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)



Thanks in advance.



-

Re:CWinThread::CreateThread

"Tom Sherren" <no.address@sorry.com.zzz>wrote in message

Quote
Questions regarding CWinThread::CreateThread



1) Does it start a "UI" thread or a "worker" thread (or both!)? If both,

how

is this distinguished?



2) Where does thread execution begin? (A pointer to a function is not

specified on the CWinThread::CreateThread() function call.)





Annnnd....the thread proc started when you call CreateThread() is

_AfxThreadEntry() (see the MFC source).



_AfxThreadEntry() will branch to either m_pfnThreadProc if it's been

initialized (worker) or to the Run() method (UI).



I wonder how many more times it will take me to answer this somewhat

correctly :-)



Mark



--

Mark Salsbery

Microsoft MVP - Visual C++





Quote


Thanks in advance.



-

Re:CWinThread::CreateThread

Thanks everyone for the useful responses.



Man, that was quick!

-