CreateThread()

Syntax

Thread = CreateThread(@ProcedureName(), *Value)
Description
Creates a new thread running in the application background. If the thread is correctly created, it returns the Thread number which is used with the other thread functions, such as KillThread(), PauseThread(), etc. The procedure which you use as a thread must take one parameter and cannot return anything. The '*Value' argument of CreateThread() is passed as the parameter to the procedure. If you do try to return a value from your thread it will simply be lost.

Parameters

@ProcedureName() The address of the procedure you want to use as the code for the new thread. Remember to put the @ in front to get the name and the () afterwards so it gets the address of the procedure.
*Value The value passed to the thread procedure as parameter. It is up to you to decide what this is used for.

Return value

The number for the newly created thread, or zero if a new thread could not be created. This number is required if you want to control the thread using the other functions in this library.

Example

The example below shows the basic way to create a thread, although in this case it does not do anything.

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

  CreateThread(@YourProcedure(), 23)

Example: Passing multiple parameters to a thread

  Structure Person
    Name$
    Age.b
    Phone.l
  EndStructure
  
  Procedure Thread(*Parameters.Person)
    
    ; Display the parameters
    ;
    Debug *Parameters\Name$
    Debug *Parameters\Age
    Debug *Parameters\Phone
    
    ; Once we don't need them anymore, use ClearStructure() to ensure dynamic 
    ; objects (if any) are correctly cleared, and release the dynamic memory block
    ClearStructure(*Parameters, Person)
    FreeMemory(*Parameters)
    
  EndProcedure
  
  ; We use a dynamically allocated block, so even if we call it from a procedure, it will
  ; still work. The memory block will be freed by the thread, when 
  ;
  *Parameters.Person = AllocateMemory(SizeOf(Person))
  *Parameters\Name$ = "John"
  *Parameters\Age   = 30
  *Parameters\Phone = 10203040
  
  CreateThread(@Thread(), *Parameters) ; Send the thread a pointer to our structure

  Delay(2000)

Supported OS

All

<- CreateSemaphore() - Thread Index - FreeMutex() ->