[fpc-devel] Delphi's current TThread implementation

Nicholas Ring nring at southcom.com.au
Mon Dec 17 07:27:33 CET 2012

 From XE3...

On 17/12/2012 3:41 AM, Sven Barth wrote:
> CheckSynchronize (in respect to entries queued by TThread.Queue; see 
> below)

See below?

> TThread.CurrentThread (or more precise: TThread.GetCurrentThread)

if the current thread (private variables) is nil then
   create an external thread object.
   if external thread list is nil then
     create an external thread list
     Atomic Compare Exchange with the 'global' list. If the result is 
not nil,
       free the external thread list that was just created.'
     add the newly created external thread object to the external thread 
     Set the private current thread object to be the newly created 
external thread object.
return private current thread object

Not sure if this is getting close on infringing on things....

> TThread.GetIsSingleProcessor (I suspect it's just a "Result := 
> FProcessorCount = 1")
Close. Checks to see if FProcessorCount is less than two (I hope it is 
never zero :-D ).

> TThread.SpinWait

Checks to see if parameter is zero, if so, exit.
Loops parameter times, calling PAUSE instruction (for Intel compatible 

> TThread.TerminatedSet

Empty method.

> TThread.CheckThreadError

If the parameter is non-zero, raise an EThread exception.

> TThread.NameThreadForDebugging (I don't think that we'll implement 
> that, but I'm curious ;) )

If a debugger is present, raise a specific exception with pointer to a 
structure containing the name for the thread. 
mentions that Delphi uses the same exception value as visual studio.

> Some further questions regarding Delphi's threading implementation:
> Does CheckSynchronize walk the whole queue or just a specific amount?

Seems to be the whole queue (but I am not sure).

> What does CheckSynchronize do if an exception happens in one of the 
> queued methods?

If it is not queued, it attaches the exception object to the item, 
otherwise re-raises.

> Does TThread.CurrentThread return something for externally created 
> threads?

Don't quite understand the question.

> Where/How is TThread.FExternalThread set?

Only when an external thread object is created.

> Where/How is TThread.FProcessorCount set?

Gotten from system CPUCount variable (which is set from Windows API 

> Is RemoveQueuedMethods called if the thread terminates?

RemoveQueuedEvents is called in the TThread destructor.

I hope that what I have done is helpful (and doesn't infringe on 


More information about the fpc-devel mailing list