why if I have used MFC in my EXE - then I cannot use

_beginthread / _beginthreadex?

to create a thread?

I seem to hear someone said so earlier...

Re: MFC - _beginthreadex by Doug

Doug
Wed Apr 16 20:14:07 CDT 2008

On Wed, 16 Apr 2008 17:34:24 -0700, Jim Johnson <aopiyy001@yahoo.com>
wrote:

>why if I have used MFC in my EXE - then I cannot use
>
>_beginthread / _beginthreadex?
>
>to create a thread?
>
>I seem to hear someone said so earlier...

If your thread uses MFC, it's best to use AfxBeginThread, and for some
guidance on using it correctly, see:

http://members.cox.net/doug_web/threads.htm

Also, the existence of _beginthread should be ignored; always use
_beginthreadex instead of _beginthread, because the former returns the
thread handle and doesn't auto-delete it. That's important for the same
reasons as described in Q1 and Q2 in the cited article.

--
Doug Harrison
Visual C++ MVP

Re: MFC - _beginthreadex by Scott

Scott
Wed Apr 16 21:32:08 CDT 2008

"Jim Johnson" <aopiyy001@yahoo.com> wrote in message
news:6m6d0496ufdtd7l89vi6tj1nbv9jdjkc6q@4ax.com...
> why if I have used MFC in my EXE - then I cannot use
>
> _beginthread / _beginthreadex?
>
> to create a thread?
>
> I seem to hear someone said so earlier...

MFC relies on some thread-local variables and state. AfxBeginThread
initializes those, then calls _beginthreadex for you. Just part of the
preconditions for using the library. If you don't use AfxBeginThread some
of your calls to MFC functions could encounter uninitialized pointers.

--
Scott McPhillips [VC++ MVP]


Re: MFC - _beginthreadex by Jim

Jim
Wed Apr 16 23:57:34 CDT 2008

What if I am creating a DLL with function like

XMLClass::DownloadData(){
// create a new thread here for as Download takes 10mins
_beginthreadex( //my thread entry point);
}


But I am not sure if my friend's application using my DLL is going to
use MFC or not.

Then should I use AfxBeginThread or _beginthreadex in my DLL library
function???


>
>MFC relies on some thread-local variables and state. AfxBeginThread
>initializes those, then calls _beginthreadex for you. Just part of the
>preconditions for using the library. If you don't use AfxBeginThread some
>of your calls to MFC functions could encounter uninitialized pointers.

Re: MFC - _beginthreadex by Scott

Scott
Thu Apr 17 08:38:13 CDT 2008

"Jim Johnson" <aopiyy001@yahoo.com> wrote in message
news:ovld04l00vru0lee29hrtjhlhbiv90123a@4ax.com...
> What if I am creating a DLL with function like
>
> XMLClass::DownloadData(){
> // create a new thread here for as Download takes 10mins
> _beginthreadex( //my thread entry point);
> }
>
>
> But I am not sure if my friend's application using my DLL is going to
> use MFC or not.
>
> Then should I use AfxBeginThread or _beginthreadex in my DLL library
> function???


You should use AfxBeginThread if, and only if, your DLL uses MFC. The
calling exe is irrelevant to this.

--
Scott McPhillips [VC++ MVP]