Page 1 of 4

Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 3:06 pm
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.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 4:51 pm
by netmaestro
Curious as to what you need it for.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 5:04 pm
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.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 6:18 pm
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

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:19 pm
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

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:21 pm
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.

Re: Get own threadID without passing as parameter

Posted: Thu Apr 30, 2020 9:25 pm
by LiK137
Hi NicTheQuick,
Thanks very much.
But if possible for windows.

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