Get own threadID without passing as parameter

Just starting out? Need help? Post your questions and find answers here.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Get own threadID without passing as parameter

Post by LiK137 »

Hi,
Same posts from different users have been done many times but none solved.
Is there any way to get own ThreadID from inside thread, just like ProgramFileName()?
If anyone knows that this is impossible also thanks for reply.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Get own threadID without passing as parameter

Post by netmaestro »

Curious as to what you need it for.
BERESHEIT
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 »

Yes... Very curious. Maybe I should remove 1 of 4,7 square meter of living surface to try to understand the psychological mechanism which bring such these questions with such a sentence as << Same posts from different users have been done many times but none solved >>.

@Lik137

Yes, there is a way. I published this way obviously, and certainly I am not the alone person to publish it.

Could you give more details about the same posts from different users ?

This allows me to stamp a link in each of these post you talk about.
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Get own threadID without passing as parameter

Post by NicTheQuick »

On Linux it works like so:

Code: Select all

Procedure test(dummy.i)
	Debug "Inside thread: " + pthread_self_()
EndProcedure

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

Debug "Outside thread: " + thread

Delay(1000)
Output:
Outside thread: 139836075214592
Inside thread: 139836075214592
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
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 »

Hello NicTheKick,

thank you for your help.

I would proceed as below x64, but I let you have the right ref.

Code: Select all

Procedure Zot(void.Q)

   Define uit.Q
   ! mov [p.v_uit], rax

EndProcedure
Last edited by Olliv on Thu Apr 30, 2020 9:21 pm, edited 1 time in total.
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 netmaestro,
Thank You for reply.

This is for passing commands from clients to server and putting into queue.
Each command is handled and put into list/map in structure
Then for each command new thread is being created.
In this thread I want to link CommandID with threadID so each thread handles it's own Command.
That is it.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Get own threadID without passing as parameter

Post by LiK137 »

Hi NicTheQuick,
Thanks very much.
But if possible for windows.
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: 6883
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: 6883
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: 3331
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: 6883
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
Post Reply