Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Approaches to threading

Author: Russell Reagan

Date: 21:27:58 06/15/04

Go up one level in this thread


On June 15, 2004 at 21:16:25, Anthony Cozzie wrote:

>On June 15, 2004 at 19:10:33, Russell Reagan wrote:
>
>>On June 15, 2004 at 18:01:07, Sean Empey wrote:
>>
>>>Since you are writing it in C look at: _beginthread
>>
>>Actually, you should use _beginthreadex() and _endthreadex(). CreateThread()
>>links to the single threaded C run time libraries, while _beginthread() and
>>_beginthreadex() link to the multithreaded C run time libraries. If you know you
>>are using the C run time libraries, you should use _beginthreadex(). If you
>>think you aren't using the C run time libraries, you should still use
>>_beginthreadex(), because it is almost impossible to know for sure that some
>>other library isn't using the C run time library under the hood.
>>_beginthreadex() is to be preferred over _beginthread() because _beginthread()
>>can cause an unrecoverable race condition in some cases. To be safe, use
>>_beginthreadex() and _endthreadex().

>I am not saying you are wrong, but wouldn't it be incredibly moronic to have a
>function that creates a thread link to the single-threaded run time libraries?
>
>anthony

Yes, I agree it doesn't make sense. That is what I read in the book,
"Multithreading Applications in Win32", by Beveridge and Wiener. Here are some
other sources. These links indicate that _beginthreadex() and _endthreadex()
should be preferred when using the C runtime library.

http://support.microsoft.com/default.aspx?scid=kb;en-us;104641

"...using CreateThread() in a program that uses Libcmt.lib causes many CRT
functions to fail."

"To guarantee that all static data and static buffers allocated by the CRT are
cleaned up when the thread terminates, _beginthreadex() and _endthreadex()
should be used when creating a thread."

"Threads that are created and terminated with the CreateThread() and
ExitThread() Win32 API functions do not have memory that is allocated by the CRT
for static data and static buffers cleaned up when the thread terminates."

"Using CreateThread() in a program that uses the CRT (for example, links with
LIBCMT.LIB) may cause a memory leak of about 70-80 bytes each time a thread is
terminated."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_C_Run.2d.Time_Library_Functions_for_Thread_Control.asp

"Warning   If you are going to call C run-time routines from a program built
with LIBCMT.LIB, you must start your threads with the _beginthread function. Do
not use the Win32 functions ExitThread and CreateThread. Using SuspendThread can
lead to a deadlock when more than one thread is blocked waiting for the
suspended thread to complete its access to a C run-time data structure."

"The _beginthread function is similar to the CreateThread function in the Win32
API but has these differences:
...
The _beginthread function initializes certain C run-time library variables. This
is important only if you use the C run-time library in your threads."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_c_run.2d.time_library_functions_for_thread_control.asp

"Warning   The multithread library LIBCMT.LIB includes the _beginthread and
_endthread functions. The _beginthread function performs initialization without
which many C run-time functions will fail. You must use _beginthread instead of
CreateThread in C programs built with LIBCMT.LIB if you intend to call C
run-time functions."

So basically if you are using the C runtime, _beginthread() is preferred over
CreateThread(), and _beginthreadex() is further preferred over _beginthread().
The book also says that the handle returned by _beginthread() cannot safely be
used.

Welcome to the world of multithreading :)



This page took 0.02 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.