Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-09

GetThreadId, GetCurrentThreadId and MainThreadId

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders

Hi,

While investigating http://bugs.freepascal.org/view.php?id=14335 I ran into the following peculiarities. Please, comment and tell for which item you want bug reports, if any. I tested on i386-linux.

1. The docs of GetThreadId seems to contain a typo: http://lazarus-ccr.sourceforge.net/docs/rtl/system/getthreadid.html "GetThreadID returns the current process ID." -> should be thread ID.

2. The docs of MainThreadID say it is unused. But it is used in TThread.Synchronize. http://lazarus-ccr.sourceforge.net/docs/rtl/classes/mainthreadid.html

Given the following program:
program project1;
{$mode objfpc}{$H+}
uses
   //cthreads,
   Classes;
begin
   writeln('GetThreadID=', GetThreadID);
   writeln('GetCurrentThreadID=', GetCurrentThreadId);
   writeln('MainThreadID=', MainThreadID);
end.

3. If cthreads is not used, all values are 1. No problem with that, although I am not sure those are real thread id, at least on i386-win32 the value changes on each run, e.g. 1908, 576, maybe because threadmanager is already included in windows. Anyway, this is not a problem, as long as they all have the same value in a single threaded application

If cthreads is used, I get the following output:
GetThreadID=3084795456
GetCurrentThreadID=3085236992
MainThreadID=3085236992

4. What is the difference between GetThreadID and GetCurrentThreadID? This code runs in the same thread, so I expect the same value.

Vincent

JoshyFun
to Vincent Snijders

Hello FPC-Pascal,

Tuesday, September 8, 2009, 2:02:46 PM, you wrote:

VS> If cthreads is used, I get the following output: VS> GetThreadID=3084795456
VS> GetCurrentThreadID=3085236992
VS> MainThreadID=3085236992

VS> 4. What is the difference between GetThreadID and VS> GetCurrentThreadID? This code runs
VS> in the same thread, so I expect the same value.

Hmmm... It looks to me as GetThreadID return the current thread handle instead the ID. In Windows both are different things but I do not know in other OS.

Anyway the name is badly choosen as it conflicts with Windows GetThreadID(ThreadHandle) function.

Best regards, JoshyFun
Vincent Snijders
to JoshyFun
JoshyFun wrote 17 lines
Hello FPC-Pascal, Tuesday, September 8, 2009, 2:02:46 PM, you wrote: VS> If cthreads is used, I get the following output: VS> GetThreadID=3084795456 VS> GetCurrentThreadID=3085236992 VS> MainThreadID=3085236992 VS> 4. What is the difference between GetThreadID and VS> GetCurrentThreadID? This code runs VS> in the same thread, so I expect the same value. Hmmm... It looks to me as GetThreadID return the current thread handle instead the ID. In Windows both are different things but I do not know in other OS.

I am not convinced, on windows they are the same, on i386-linux they are different.

Vincent