unit initialization

Discussion and questions about programming with Ultibo.
Koen
Posts: 26
Joined: Sat Apr 29, 2017 10:41 am

unit initialization

Postby Koen » Tue Jun 19, 2018 9:22 am

If I add a startup-code to the end of a unit like e.g.

begin
HTTPListener:=THTTPListener.Create;
HTTPListener.Active:=True;
WebStatusRegister(HTTPListener,'','',True);
end.

should I add a
ThreadHalt(0);
More general: is the start-code of all the units started in one thread or single threads?
Just for interrest.
User avatar
Ultibo
Site Admin
Posts: 1903
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: unit initialization

Postby Ultibo » Tue Jun 19, 2018 12:05 pm

Koen wrote:should I add a
ThreadHalt(0);

No, that would halt the system during startup, see below for more details.

Koen wrote:More general: is the start-code of all the units started in one thread or single threads?
Just for interrest.

All unit initialization code is executed by the main thread during startup, at the point this occurs all of the platform and threading initialization has already been completed and things like timer and scheduler interrupts are already running.

If you look at the MainExecute function in the Threads unit you will see that it calls PASCALMAIN which is actually the FPC internal name for the begin/end block in the main program unit.

There is no one place where you can see the code for how initialization occurs but the general concept is that at the start of PASCALMAIN (the program begin/end block) an invisible function is called that walks through an array containing the initialization point for every unit.

At the end of PASCALMAIN another invisible function calls the finalization code of each unit and that is the reason we add ThreadHalt(0) to the end of all of our examples, if the main thread ever exited the program begin/end block it could call finalization functions while the system is still running (although there are some other protections as well just in case).

Hope that makes sense.
Ultibo.org | Make something amazing
https://ultibo.org

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest