[fpc-pascal] GetCurrentThreadID under FreeBSD vs Linux

Graeme Geldenhuys graeme at geldenhuys.co.uk
Fri Feb 8 15:40:47 CET 2013


Hi,

Under Linux and Windows I have the following code which works fine.

var
  LThreadID: string;
begin
  FmtStr(LThreadID, '%.4d', [GetCurrentThreadID]);


Under FreeBSD (64-bit) that failed with a EConvertError in the unit
tests and the compiler gave a message of 'Invalid argument index in
format "%.4d"'

Navigating the code to see how TThreadID is defined, I found this for
FreeBSD.

  TThreadRec = record end;
  TThreadID  = ^TThreadRec;

So TThreadID is just a pointer to a record structure. Apparently getting
a "real" thread ID/number is not as easy as under Linux. [Info from
Google searches]. Under FreeBSD it seems that naming each thread with a
string value is a more supported solution.

I'm not so fussed about the exact ID per thread, so the pointer value
will be fine. Windows uses a THandle and Linux uses a PtrUInt as the
types for TThreadID.

So would it be safe if I did the following in my code? For the Windows,
Linux and FreeBSD platforms at least.

  FmtStr(LThreadID, '%.4d', [PtrUInt(GetCurrentThreadID)]);


I've tested the PtrUInt() cast under FreeBSD, Linux and Windows, and it
seems to work fine.


BTW:
This code is only used to supply a thread id/value to a logging function
(file, GUI, console etc) to help with debugging, or at least show which
thread wrote what debug log entries. So it's not critical code.



Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/




More information about the fpc-pascal mailing list