To create a thread in Delphi, be sure to use Beginthread () instead of CreateThread () to create the thread! (Better management of exceptions)

Source: Internet
Author: User

To create a thread in Delphi, be sure to use Beginthread () instead of CreateThread () to create the thread!
If you create multiple threads directly using Win32 's API function CreateThread (), you can create them as well. However, you should understand that the dynamic allocation and destruction of memory blocks in each thread requires synchronous protection. Delphi language has a very important in the use of multi-threaded environment of the global variable Ismultithread, the system in memory allocation, according to the value of the Ismultithread variable to determine whether the current use in a multithreaded environment, if the variable is true, which, When allocating and destroying memory, the system is to be protected synchronously. Instead, there is no synchronization protection.
So, if you create a thread directly using CreateThread (), be sure to manually set the Ismultithread variable to true. Otherwise you will often find the system memory illegal access Error! If you create a thread using Beginthread () in Delphi, the implementation of this beginthread () is as follows:
function Beginthread (securityattributes:pointer; Stacksize:longword;
Threadfunc:tthreadfunc; Parameter:pointer; Creationflags:longword;
var threadid:longword): Integer;
Var
P:pthreadrec;
Begin
New (P);
P.func: = ThreadFunc;
P.parameter: = Parameter;
Ismultithread: = TRUE;
Result: = CreateThread (Securityattributes, StackSize, @ThreadWrapper, P,
Creationflags, ThreadID);
End
Did you see it? The above already ismultithread: = TRUE; This guarantees the security of memory usage under multithreading.
Another important reason to replace CreateThread () with the Beginthread () function is in the code snippet above, do you find it?  Beginthread () function inside the call CreateThread (), which thread function pointer is @threadwrapper, the parameter is P, and P:pthreadrec; is a struct pointer, the inside of the struct is the thread function and the thread function parameter respectively.
The Threadwrapper function is implemented as follows:
{$IFDEF MSWindows}
function Threadwrapper (parameter:pointer): Integer; stdcall;
{$ELSE}
function Threadwrapper (parameter:pointer): Pointer; Cdecl
{$ENDIF}
Asm
{$IFDEF Pc_mapped_exceptions}
{Mark The top of the stack with a signature}
PUSH Unwindfi_topofstack
{$ENDIF}
Call _fpuinit
PUSH EBP
{$IFNDEF Pc_mapped_exceptions}
XOR ECX,ECX
PUSH Offset _exceptionhandler
MOV EDX,FS:[ECX]
PUSH EDX
MOV Fs:[ecx],esp
{$ENDIF}
{$IFDEF Pc_mapped_exceptions}
The signal handling code in Sysutils depends on being able to
Discriminate between Delphi threads and foreign threads in order
To choose the disposition of certain signals. It does this by
Testing a TLS index. However, we allocate TLS in a lazy fashion,
So this test can fail unless we ' ve already allocated the TLS segment.
So we force the allocation of the TLS index value by touching a TLS
Value here. So don ' t remove the silly call to areosexceptionsblocked.
Call areosexceptionsblocked
{$ENDIF}
MOV Eax,parameter
MOV Ecx,[eax]. Tthreadrec.parameter
MOV Edx,[eax]. Tthreadrec.func
PUSH ECX
PUSH EDX
Call _freemem
POP EDX
POP EAX
Call EDX

{$IFNDEF Pc_mapped_exceptions}
XOR Edx,edx
POP ECX
MOV FS:[EDX],ECX
POP ECX
{$ENDIF}
POP EBP
{$IFDEF Pc_mapped_exceptions}
{Ditch our TOS marker}
ADD ESP, 4
{$ENDIF}
End

The Threadwrapper function is implemented using BASM assembler code, and if you are familiar with Basm assembly, you know that the preceding code is the Convention of the function arguments in the BASM assembly, and the following is the focus, which internally implements the mechanism of converting to an exception when executing a thread function exception error.

If you create a thread directly using CreateThread (), there is certainly no benefit of executing the thread function providing the triggering exception, which is the most important reason to use the beginthread () function instead of CreateThread (), and also the root cause.

Is the exception mechanism in the thread important? Of course it matters! Because the thread function is just a body of execution function, when an exception occurs during the execution of this function, the program exits the current thread function, that is, the thread terminates. So it is very important to provide the exception mechanism in the thread and let us capture the processing in the thread.

http://blog.csdn.net/zang141588761/article/details/51654748

To create a thread in Delphi, be sure to use Beginthread () instead of CreateThread () to create the thread! (Better management of exceptions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.