solution that causes the program to die due to the re-entry of the Delphi main thread

Source: Internet
Author: User

The Delphi thread can run synchronously in the main thread by calling Athread.synchronize (Aproc), where Athread will hang until the main thread finishes executing aproc.

If there is Bthread, Bthread.synchronize (Bproc) is called, and Athread is released in Bproc

procedure Tbthread.bproc begin   athread.terminate;  athread.waitfor;  Athread.free; End;

At this point our program will be stuck, the following code can avoid deadlocks, is a generic thread waiting for the end code.

    athread.terminate;      while  not  do    begin      if then  //Because it is performed through synchronize synchronization to the main thread, call Checksynchronize to prevent deadlocks         checksynchronize ( 0 );      Sleep (1);     End ;    Freeandnil (athread);

If you have not noticed this re-entry problem before, please modify your code. Because there is no guarantee that our code will not be re-entered when it is taken by someone else.

This code supports both Windows and Android

However, the units involved are not the same:

uses System.syncobjs, {$ifdef mswindows} Windows; {$endif} {$ifdef POSIX} Posix.pthread; {$endif}

solution that causes the program to die due to the re-entry of the Delphi main thread

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.