ThreadPriority()

Syntax

OldPriority = ThreadPriority(Thread, Priority)
Description
Change the priority of the specified thread and returns the old priority.

The priority value can go from 1 to 32. 1 is the lowest priority available, 16 is the normal priority and 32 is the time critical priority (highest, please don't use it unless you know what you're doing).

If the priority is 0, then the priority isn't changed (useful to only retrieve the thread priority without change it). All threads are created with a normal priority.

Typically you would give a thread which is always running (for example, an image processing thread) a priority which is not greater than any other thread in your system. The reason for this is that if it is always running, and it has a high priority, then no other thread will get a chance to run. If a thread needs to be highly responsive but spends most of its time waiting for some event you might consider giving it a higher than normal priority.

Windows schedules threads (chooses which one to run for a short length of time) using a preemptive priority based scheduling strategy, with round robin scheduling within the same priority level. This means that if it is time for execution of a different thread (known as a context switch) then the thread with the highest priority that is available to run will be the next one to be executed. If there is more than one thread with the highest priority, and they are available to run, then each thread will be cycled through on successive context switches.

Parameters

Thread The thread to change the priority of. This value is returned by CreateThread().
Priority The new priority to assign to the thread. The priority can be zero (meaning not to change the priority) or range from 1 (lowest priority) to 32 (highest priority). 16 is normal priority. Windows doesn't support 32 different level of priority, here is the corresponding table:
  - 1: lowest
  - between 2 and 15: below normal 
  - 16:  normal
  - between 17 and 30: above normal
  - 31: highest
  - 32: time critical

Return value

The priority of the thread before this function was called. This can be useful if you only want to boost the priority of the thread for a short time and then return it to its previous level. The returned value is not necessary the same than the one set with ThreadPriority(), as it depends of the granularity of the priority tuning offered by the system.

Example

  ; Procedure which always runs (note, no Delay function as
  ; this would cause the thread to stop running while it was
  ; being delayed
  Procedure PrintStuff(*Interval)
    For i = 0 To 1000000000
      ; Nasty busy wait
    Next
  EndProcedure
  
  If OpenConsole()
    thread = CreateThread(@PrintStuff(), 500)
    If thread
      ; Increase the priority above the main thread
      ; You should notice a delay before the print function
      ; is executed. Now change the 17 to 15 (lower than normal priority)
      ; and see that the print executes immediately
      ThreadPriority(thread, 17)
      PrintN("Waits for higher priority thread to finish")
    EndIf
    
    PrintN("Press return to exit")
    Input()
  EndIf

Supported OS

Windows

<- ThreadID() - Thread Index - TryLockMutex() ->