Page 1 of 1

Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 5:22 am
by Randy Walker
I'm Baaaack. Looking into threads and wondering why help page on it shows asterisk prefix on parameter name, but thread.pb does not have the asterisk. Must be something I'm missing here.

The help page shows this:

Code: Select all

  Procedure YourProcedure(*Value)    ; The variable '*Value' will contain 23
  EndProcedure

  CreateThread(@YourProcedure(), 23)
Has asterisk. This asterisk has no special meaning? Just part of the variable name?

The thread.pb shows this:

Code: Select all

Procedure AlertThread(Parameter)

  Repeat
    Debug "Alert !"
    Delay(3000)
  ForEver

EndProcedure

CreateThread(@AlertThread(), 154)
No asterisk. So, it does not appear to be required as indicated in the help page.

Can someone explain this?

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 7:05 am
by DarkDragon
A pointer is an integer pointing to an address in memory. If you just need an integer value you can also decide to just pass an integer. If you need a memory address you would use a pointer (with asterisk).

Integer (.i) and pointers (*) have the same size.

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 7:16 am
by Randy Walker
DarkDragon wrote: Sun Mar 30, 2025 7:05 am A pointer is an integer pointing to an address in memory. If you just need an integer value you can also decide to just pass an integer. If you need a memory address you would use a pointer (with asterisk).

Integer (.i) and pointers (*) have the same size.
So I think what you are saying is the thread parameter can pass either one, a variable or a pointer.

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 8:58 am
by infratec
A parameter of the type integer or a pointer to ,,, normaly a structure.

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 9:09 am
by BarryG
The asterisk is just part of the variable name and nothing more. People use it to identify a variable as holding a pointer to memory, kind of like using "$" on a string instead of ".s" for it. Just so anyone reading the code knows what type of value it holds.

Code: Select all

var=1
*var=2

Debug var ; 1
Debug *var ; 2

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 11:55 am
by mk-soft
Thread threads are used to process long-term tasks in the background, or to distribute the same tasks.
It is better to pass a pointer to the data to the thread instead of accessing global data.
This means that a thread procedure can be used several times for the same task.

Update

Code: Select all

; Threads

EnableExplicit

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Use Compiler Option Threadsafe!"
CompilerEndIf

Structure udtWork
  ThreadID.i
  iVal1.i
  iVal2.i
  iResult.i
EndStructure

Structure udtWorkData
  ThreadID.i
  Exit.i
  ; Data
  Count.i
EndStructure


Procedure thWork(*data.udtWork)
  ; Do Init
  Delay(100)
  
  ; Do Work
  *data\iResult = *data\iVal1 * *data\iVal2
  
  ; Do Release
  Delay(100)
EndProcedure

Procedure thWorkBackground(*data.udtWorkData)
  ; Do Init
  Delay(10)
  
  While Not *data\Exit
    ; Do Work
    *data\Count + 1
    ; Safe CPU Power
    Delay(10)
  Wend
  
  ; Do Release
  Delay(10)
EndProcedure


Global calc1.udtWork, calc2.udtWork
Global MyWorkData.udtWorkData

calc1\iVal1 = 10
calc1\iVal2 = 5

calc2\iVal1 = 10
calc2\iVal2 = 10

calc1\ThreadID = CreateThread(@thWork(), @calc1)
calc2\ThreadID = CreateThread(@thWork(), @calc2)
MyWorkData\ThreadID = CreateThread(@thWorkBackground(), @MyWorkData)

; Stop: wait thread done
WaitThread(calc1\ThreadID)

; Loop: wait thread done
While IsThread(calc2\ThreadID)
  ; Do any
  Delay(10)
Wend

Debug "Result 1 = " + calc1\iResult
Debug "Result 2 = " + calc2\iResult

; Exit Program
MyWorkData\Exit = #True
If WaitThread(MyWorkData\ThreadID, 5000) = 0
  Debug "WorkData hangs. Killed"
  KillThread(MyWorkData\ThreadID)
Else
  Debug "WorkData Done: Count = " + MyWorkData\Count
EndIf

Re: Thread parameter W/WO asterisk

Posted: Sun Mar 30, 2025 3:45 pm
by NicTheQuick
BarryG wrote: Sun Mar 30, 2025 9:09 am The asterisk is just part of the variable name and nothing more. People use it to identify a variable as holding a pointer to memory, kind of like using "$" on a string instead of ".s" for it. Just so anyone reading the code knows what type of value it holds.

Code: Select all

var=1
*var=2

Debug var ; 1
Debug *var ; 2
No, it is not the same. Look at this simple example:

Code: Select all

Define var.Integer
Define *var.Integer

var\i = 1
Debug var\i

*var\i = 1
Debug *var\i