Page 1 of 1

[No it's not] Is it a bug ? (ThreadID() in windows)

Posted: Thu Apr 30, 2020 10:19 pm
by infratec
Hi,

in an other thread something strange occured:

Code: Select all

Procedure test(dummy.i)
  Debug "CurrentProcessId: " + GetCurrentProcessId_()
  Debug "CurrentThreadId : " + GetCurrentThreadId_()
  ;Debug "CurrentThread   : " + GetCurrentThread_()
 
  Delay(20000)
 
EndProcedure


Define Thread.i = CreateThread(@test(), 0)

Debug "Outside Thread: " + ThreadParameter\Thread
Debug "Outside ThreadID: " + ThreadID(ThreadParameter\Thread)

Delay(25000)
Results in:

Image

There is nothing to see of 364 :!:

Is it a bug, or what exactly returns ThreadID() :?:

PB 5.72 x86 Win10 x64

Re: Is tis a bug ? (ThreadID() in windows)

Posted: Thu Apr 30, 2020 11:57 pm
by netmaestro
The OS knows what it is. If we pass the result of PB's ThreadID() function to the OS to do something with, it happens:

Code: Select all

Procedure ThisIsAThread(void)
  Repeat
    Debug "I'm a thread!"
    Delay(1000)
  ForEver
EndProcedure

tid = CreateThread(@ThisIsAThread(), 0)
Delay(2000)
tid2 = ThreadID(tid) 
If TerminateThread_(tid2, 13)
  Debug "No you're not..."
EndIf
Delay(5000)
GetExitCodeThread_(tid2, @exitcode.i)
Debug exitcode

Re: Is tis a bug ? (ThreadID() in windows)

Posted: Fri May 01, 2020 9:36 am
by freak
It is a handle to the thread, just like ImageID() returns an image handle. The documentation mentions this.

Re: Is tis a bug ? (ThreadID() in windows)

Posted: Fri May 01, 2020 9:43 am
by Shardik
freak wrote:It is a handle to the thread, just like ImageID() returns an image handle. The documentation mentions this.
I am just a moment too late. Please see my example in this thread.

Re: Is tis a bug ? (ThreadID() in windows)

Posted: Fri May 01, 2020 9:50 am
by infratec
@Freak : Thank you

Here is the right interpretation:

Code: Select all

Import ""
  GetThreadId.l(Handle.i)
EndImport



Procedure test(dummy.i)
  Debug "CurrentProcessId      : " + GetCurrentProcessId_()
  Debug "CurrentThreadId       : " + GetCurrentThreadId_()
  Debug "Virtual CurrentThread : " + GetCurrentThread_()
  
  Debug "CurrentThreadId by OS via virtual CurrentThread : " + GetThreadID(GetCurrentThread_())
  
  Delay(20000)
 
EndProcedure


Define Thread.i = CreateThread(@test(), 0)

Debug "Outside Thread: " + Thread
Debug "Outside ThreadID PB (not the ThreadID, only a HANDLE) : " + ThreadID(Thread)
Debug "Outside ThreadID OS: " + GetThreadID(ThreadID(Thread))

Delay(25000)