Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Windows _beginthread vs. CreateThread

3 views
Skip to first unread message

Steve

unread,
Apr 23, 2002, 10:18:48 AM4/23/02
to
What is the difference between _beginthread and CreateThread? When
would I want to use one vs. the other? Why have two different ones?
Is it for backward compatibility?

Sorry if this is off topic.

Thanks for the info.
Steve

Arnold Hendriks

unread,
Apr 23, 2002, 10:45:18 AM4/23/02
to
Steve <stev...@earthlink.net> wrote:
> What is the difference between _beginthread and CreateThread? When
> would I want to use one vs. the other? Why have two different ones?
> Is it for backward compatibility?
CreateThread is the Win32 API function. _beginthread is the runtime
library's wrapper for CreateThread. You should use _beginthread, because
that gives the runtime library a chance to know that you have started
a new thread, and perhaps initialize thread-specific storage.

--
Arnold Hendriks <a.hen...@b-lex.com>
B-Lex Information Technologies, http://www.b-lex.com/

Alexander Terekhov

unread,
Apr 23, 2002, 11:00:26 AM4/23/02
to

Steve wrote:
>
> What is the difference between _beginthread and CreateThread? When
> would I want to use one vs. the other? Why have two different ones?

Are you programming in C/C++? If so...

Both are totally brain-damaged! Instead, use pthread_create():

http://sources.redhat.com/pthread-win32

regards,
alexander.

P.S. And don't miss cond.vars and other pthreads stuff too! ;-)

Alexander Terekhov

unread,
Apr 23, 2002, 11:05:04 AM4/23/02
to

Alexander Terekhov wrote:
>
> Steve wrote:
> >
> > What is the difference between _beginthread and CreateThread? When
> > would I want to use one vs. the other? Why have two different ones?
>
> Are you programming in C/C++? If so...
>
> Both are totally brain-damaged! Instead, use pthread_create():
>
> http://sources.redhat.com/pthread-win32

Oops! I meant: http://sources.redhat.com/pthreads-win32

Eric D Crahen

unread,
Apr 23, 2002, 12:09:29 PM4/23/02
to
CreateThread() will give you a thread, but won't do anything to let the
rest of the c runtime know your using a thread - so the parts that aren't
thread aware in the runtime will get screwed up unless you are very
careful about what you're doing.

_beginthreadex() will also give you a thread, but it will do some extra
work so the runtime knowns you are working with threads - which eliminates
the problems CreateThread() might introduce.

It is a little like the difference between compiling a unix program using
threads with the -DREENTRANT flag and compiling the same program w/o that
flag. There are alot of short articles about this, and I think there are
even one or two in the MSDN but I could be mistaken.

- Eric
http://www.cse.buffalo.edu/~crahen

Steve

unread,
Apr 23, 2002, 1:23:38 PM4/23/02
to
stev...@earthlink.net (Steve) wrote in message news:<21b63736.02042...@posting.google.com>...

I did a little research (I know I should have done that first) and
found an old posting, which refers to this issue. The post provides
this advice:

> In short, _beginthread/_exitthread pair for c programs that are say console
> apps that are basic and use the c-runtime lib/dll. If it's a real Win32
> app, use Win32 API's and stay away from the c-runtime. Hope this clears it
> up somewhat.

Even the MSDN section &#8220;Writing Multithreaded Win32
Programs&#8221; refers to _beginthread (including their Bounce
example). I don&#8217;t think Bounce is a Win32 program (looks like a
console program).

Any other advice on this topic? I think there is also an MFC version
of thread control, isn&#8217;t there?

Steve

Alexander Terekhov

unread,
Apr 23, 2002, 2:45:54 PM4/23/02
to

Steve wrote:

[...MSDN/Writing Multithreaded Win32 Programs/Bounce/MFC...]

> Any other advice on this topic?

YES:
http://groups.google.com/groups?as_umsgid=c29b5e33.0201240004.57d109f4%40posting.google.com

regards,
alexander.

Patrick TJ McPhee

unread,
Apr 24, 2002, 1:42:46 AM4/24/02
to
In article <Pine.SOL.4.30.02042...@hadar.cse.Buffalo.EDU>,
Eric D Crahen <cra...@cse.buffalo.edu> wrote:

% CreateThread() will give you a thread, but won't do anything to let the
% rest of the c runtime know your using a thread - so the parts that aren't
% thread aware in the runtime will get screwed up unless you are very
% careful about what you're doing.

This is a bit of a mis-statement. If you use the MS C run-time and start
a thread with CreateThread(), you may have memory leaks when the thread
exits. Being careful won't help with this, unless you're careful enough
to avoid using the C run-time.

Having said that, it's probably best to assume what Crahen says is
strictly true, up until the word `unless'.
--

Patrick TJ McPhee
East York Canada
pt...@interlog.com

Justin Michel

unread,
Apr 25, 2002, 8:04:17 PM4/25/02
to
Some libraries (MFC, ACE/TAO, C runtime, etc.) provide thread creation
functions which you must use under certain circumstances. This is usually
because the library chooses to store something in thread-specific-storage,
and can make it difficult when using these libraries together. However, it's
a common problem, so you shouldn't have any trouble finding relevant
documentation.

HTH,

Justin Michel
OCI


Jonathan de Boyne Pollard

unread,
May 12, 2002, 2:53:13 PM5/12/02
to
EDC> CreateThread() will give you a thread, but won't do anything to let the
EDC> rest of the c runtime know your using a thread - so the parts that aren't
EDC> thread aware in the runtime will get screwed up unless you are very
EDC> careful about what you're doing.

It is the parts that _are_ thread-aware that will become screwed up.

0 new messages