Page 1 of 4

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:34 pm
by Olliv

Code: Select all

Structure Bus
   void.i
   ...
EndStructure


Procedure myProc(*B.Bus)
   With *B
       ...
   EndWith
EndProcedure


Define *B.Bus


Th0 = CreateThread(@myProc(), *B)
Repeat
   Delay(1)
Until *B\Quit
Link without map neither array to test.
https://www.purebasic.fr/french/viewtop ... 66&start=4

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:39 pm
by LiK137
Dear Olliv,
Please neglect my posts, do not waste your precious time.
You will help me if you do not reply to my posts.
Thank You very much for understanding.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:44 pm
by infratec

Code: Select all

Procedure test(dummy.i)
  Debug "Inside thread: " + GetCurrentProcessId_()
  Debug "Inside thread: " + GetCurrentThreadId_()
  
  Delay(20000)
  
EndProcedure

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

Debug "Outside thread: " + thread

Delay(25000)
According to SysInternals ProcessExplorer is GetCurrentThreadId_() the right value.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:50 pm
by LiK137
Dear infratec,

Just added "Debug "Outside2 thread: " + ThreadID(thread)" before delay(25000) to check result

Code: Select all

Procedure test(dummy.i)
  Debug "Inside thread: " + GetCurrentProcessId_()
  Debug "Inside thread: " + GetCurrentThreadId_()
 
  Delay(20000)
 
EndProcedure

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

Debug "Outside1 thread: " + thread
Debug "Outside2 thread: " + ThreadID(thread)

Delay(25000)

results:
[00:46:53] Outside1 thread: 1
[00:46:53] Outside2 thread: 184
[00:46:53] Inside thread: 516
[00:46:53] Inside thread: 7164


Inside and Outside completely different.

Thanks very much

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 10:01 pm
by infratec
I know, but I think this is a PB bug:

Image

There is nothing to see of 364 :!:

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 10:06 pm
by BarryG
LiK137 wrote:Is there any way to get own ThreadID from inside thread
Is this what you mean?

Code: Select all

Global id_thread_1, id_thread_2

Procedure Thread1(null)
  Debug "Thread 1 ID: "+Str(id_thread_1)
EndProcedure

Procedure Thread2(null)
  Debug "Thread 2 ID: "+Str(id_thread_2)
EndProcedure

id_thread_1=CreateThread(@Thread1(),0)
id_thread_2=CreateThread(@Thread2(),0)
Delay(100)

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 10:12 pm
by infratec
Extended version without global variables:

Code: Select all

Structure ThreadParameterStructure
  Thread.i
  Delay.i
EndStructure

Procedure test(*ThreadParameter.ThreadParameterStructure)
  Debug "CurrentProcessId: " + GetCurrentProcessId_()
  Debug "CurrentThreadId : " + GetCurrentThreadId_()
  ;Debug "CurrentThread   : " + GetCurrentThread_()
  
  Delay(*ThreadParameter\Delay)
  
EndProcedure

Define ThreadParameter.ThreadParameterStructure

ThreadParameter\Delay = 20000
ThreadParameter\Thread = CreateThread(@test(), @ThreadParameter)

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

Delay(25000)
Since you need (normally) always parameters, you can add them in the structure.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 10:31 pm
by LiK137
Dear infratec,
I agree with You regarding the real thread obtained by GetCurrentThreadId_()
But how to use this ThreadID with PureBasic thread library to check if running or pause or kill.
With Purebasic ThreadID() it is possible to pause or kill or check if thread running.

Thank You very much

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 10:38 pm
by LiK137
Dear BarryG,
The global variable assignment is not suitable for my post as these threads being created by networkSever procedure which may have to create more threads than global declarations.

Thank You very much.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 11:01 pm
by Olliv
@Lik137

First...

Code: Select all

Procedure Zot(void.Q)
   Define uit.Q
   ! mov [p.v_uit + 72], rax ; threadId 
   Debug uit
   Delay(2000)
EndProcedure

Debug ThreadId(createthread(@Zot(), 0) )
Delay(5000)
End
You see it is the same value inside and outside. But normally, a protecting error will be done in the future CPU. (hardware, not software)

Purebasic is a software.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 11:50 pm
by Olliv
Secondly... I do not waste my time : all is already explained above, and the source code date is 2009.

If you have technical questions about it, no problem : I will just cut extract after extract a source code part to explain it.

And it is very very easy to include the recent network process I published. Etc... Etc...

Peace my friend !!
:mrgreen:

Re: Get own threadID without passing as parameter

Posted: Fri May 01, 2020 3:12 am
by Olliv
infratec wrote:Extended version without global variables:

Code: Select all

Structure ThreadParameterStructure
  Thread.i
  Delay.i
EndStructure

Procedure test(*ThreadParameter.ThreadParameterStructure)
  Debug "CurrentProcessId: " + GetCurrentProcessId_()
  Debug "CurrentThreadId : " + GetCurrentThreadId_()
  ;Debug "CurrentThread   : " + GetCurrentThread_()
  
  Delay(*ThreadParameter\Delay)
  
EndProcedure

Define ThreadParameter.ThreadParameterStructure

ThreadParameter\Delay = 20000
ThreadParameter\Thread = CreateThread(@test(), @ThreadParameter)

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

Delay(25000)
Since you need (normally) always parameters, you can add them in the structure.
This code part is right.

I have two remarks about these recent questions.

1) I recommand dynamic pointors, even in basis (out of procedures)

Code: Select all

Structure Root

   Head.I

   Map aMap.Key()
   Array anArray.Field(0)

EndStructure

Define *R.Root = AllocateStructure(Root)
CreateThread(@ObjectCreate(), *R)
; etc...
This allows a "perfect" (near one integer) memory managing of maps and arrays.
This allows any backup, swap, levelling or thread buffer root.


2) I think about such any questions about links between thread clients and process.

Now, I realize : if anybody began his coding by single procedures with specific arguments, without bus neither common argument, so there is a real converting problem to thread these procedures if a ThreadId() function is missing.

In this way (but it was not explicit in the question), Threaded could be used to prevent from variable name conflict, Global.ThreadStructure *P to share all information between every thread. (There is just a problem of data security)

Re: Get own threadID without passing as parameter

Posted: Fri May 01, 2020 7:39 am
by LiK137
Dear Olliv,
Thank You for your reply.
Unfortunately neither this for pb5.71x64 Win7:

Code: Select all

Procedure Zot(void.Q)
   Define uit.Q
   ! mov [p.v_uit + 72], rax ; threadId
   Debug uit
   Delay(2000)
EndProcedure

Debug ThreadID(CreateThread(@Zot(), 0) )
Delay(5000)
End
nor this for pb5.71x86 Win7:

Code: Select all

Procedure Zot(void)
   Define uit
   ! mov [p.v_uit + 72], eax ; threadId
   Debug uit
EndProcedure

Define thdID = CreateThread(@Zot(), 0)
Debug ThreadID(thdID)
Delay(5000)
End
give anything close to pb ThreadID.

Re: Get own threadID without passing as parameter

Posted: Fri May 01, 2020 9:39 am
by Shardik
The documentation of ThreadID() states that the result of ThreadID() returns a handle:
[color=#0040FF][u]Documentation for ThreadID()[/u][/color] wrote:Return value

The system identifier. This result is sometimes also known as 'Handle'. Look at the extra chapter Handles and Numbers for more information.
Indeed the return parameter of ThreadID() is not the ID but the thread handle. You therefore have to convert the thread handle to a thread ID as DarkPlayer has already demonstrated. After inserting his procedure GetThreadIDFromHandle() into LiK137's previous code example, the outside ThreadID will be the same as the inside ThreadID... :wink:

Code: Select all

#ThreadBasicInformation = 0

Structure CLIENT_ID
   UniqueProcess.i
   UniqueThread.i
EndStructure

Structure THREAD_BASIC_INFORMATION
  ExitStatus.l
  TebBaseAddress.i
  ClientId.CLIENT_ID
  AffinityMask.l
  Priority.l
  BasePriority.l
EndStructure

Procedure Test(Dummy.i)
  Debug "Inside ProcessID: " + GetCurrentProcessId_()
  Debug "Inside ThreadID: " + GetCurrentThreadId_()
  Delay(10)
EndProcedure

Procedure GetThreadIDFromHandle(Handle.i)

  Protected Info.THREAD_BASIC_INFORMATION

  If NtQueryInformationThread_(Handle, #ThreadBasicInformation, @Info,
    SizeOf(Info), 0) = 0
    ProcedureReturn Info\ClientId\UniqueThread
  EndIf

EndProcedure

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

Debug "Outside ThreadID: " + GetThreadIDFromHandle(ThreadID(Thread))

Delay(10000)

Re: Get own threadID without passing as parameter

Posted: Fri May 01, 2020 9:51 am
by infratec
Hre you can find the short interpretation of PBs ThreadID()

https://www.purebasic.fr/english/viewto ... 94#p553494

Unfortunately you get only a virtual thread handle inside the thread.