Get own threadID without passing as parameter

Just starting out? Need help? Post your questions and find answers here.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Get own threadID without passing as parameter

Post 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
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post 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.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get own threadID without passing as parameter

Post 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.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post 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
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get own threadID without passing as parameter

Post by infratec »

I know, but I think this is a PB bug:

Image

There is nothing to see of 364 :!:
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Get own threadID without passing as parameter

Post 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)
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get own threadID without passing as parameter

Post 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.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post 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
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Get own threadID without passing as parameter

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Get own threadID without passing as parameter

Post 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:
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Get own threadID without passing as parameter

Post 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)
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Get own threadID without passing as parameter

Post 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)
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get own threadID without passing as parameter

Post 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.
Post Reply