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