Thread parameter W/WO asterisk

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Thread parameter W/WO asterisk

Post 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?
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Thread parameter W/WO asterisk

Post 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.
bye,
Daniel
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Thread parameter W/WO asterisk

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Thread parameter W/WO asterisk

Post by infratec »

A parameter of the type integer or a pointer to ,,, normaly a structure.
BarryG
Addict
Addict
Posts: 4121
Joined: Thu Apr 18, 2019 8:17 am

Re: Thread parameter W/WO asterisk

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Thread parameter W/WO asterisk

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1502
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Thread parameter W/WO asterisk

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