[Windows x64] Simple Thread Pool Library

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

[Windows x64] Simple Thread Pool Library

Post by Mijikai »

Simple Thread Pool Library
Written in PureBasic 6.0 (C-Backend)


A simple library to create a pool of threads, assing tasks and run them all at once.
What the SPP (viewtopic.php?p=588691#p588691) gfx library internally used for distributed rendering.


Note (05.12.2022):
There seems to be an issue if a program that uses STP itself has the threadsafe compiler flag set.
Im not sure why this is happening but i will update the thread when i have more infos.


Interface:

Code: Select all

EnableExplicit

;--------------------------------------------------------------------------------------
; STP - SIMPLE THREAD POOL Library
; CREATE A POOL OF THREADS SET THEIR TASKS AND EXECUTE THEM
; Written in PureBasic 6.0 C-Backend
;--------------------------------------------------------------------------------------
; Version: dev.01.03 (alpha 2)
; Author: Mijikai
; Platform: Windows (x64)
;--------------------------------------------------------------------------------------
; Copyright 2022 by Mijikai all rights reserved
;--------------------------------------------------------------------------------------
; License:
; Attribution-NonCommercial-NoDerivatives 4.0 International:
; https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
;--------------------------------------------------------------------------------------

Import "stp.lib"
  stpInterface.i()
  stpVersion.i()
EndImport

#STP_VERSION = $0002

Interface STP
  PoolCreate.i(Threads.i = #Null)               ;<- add threads to the pool (if zero it uses the systeminfo to create threads according to the processor) / returns the number of added threads
  PoolSize.i()                                  ;<- how many threads are in the pool
  PoolTasksActive.i()                           ;<- how many tasks are active
  PoolTasksAvailable.i()                        ;<- how many tasks are free
  PoolTaskAdd.i(*Callback,*Parameter)           ;<- get a free task and assing a callback and parameter / returns a task handle
  PoolTaskChange.i(Task.i,*Callback,*Parameter) ;<- change the function (if not zero) and parameter of a task (needs a task handle)
  PoolTaskRemove.i(Task.i)                      ;<- remove a task (needs a task handle if the handle is zero all tasks will be removed!)
  PoolExecute.i()                               ;<- execute all active tasks in the pool
  PoolWait.i()                                  ;<- wait for all tasks to be completed
  PoolExecuteWait.i()                           ;<- execute all active tasks in the pool and wait for their completion
  PoolRelease.i()                               ;<- release the thread pool (threads & tasks)
  Release.i()                                   ;<- release the interface and the thread pool (threads & tasks)
EndInterface
Download (alpha 2): https://www.dropbox.com/s/s1769pf0j50w8 ... 2.zip?dl=0

Example:

Code: Select all

EnableExplicit

XIncludeFile "stp.pbi"

Structure DRAW_STRUCT
  x.i
  y.i
  w.i
  h.i
  c.i
EndStructure

Structure TASK_STRUCT
  draw.DRAW_STRUCT[9]
EndStructure

Global task.TASK_STRUCT

Macro DrawSet(_d_,_x_,_y_,_w_,_h_,_c_)
  _d_\x = _x_
  _d_\y = _y_
  _d_\w = _w_
  _d_\h = _h_
  _d_\c = _c_
EndMacro

Procedure.i Draw(*draw.DRAW_STRUCT)
  Protected.i x,y,w,h
  With *draw
    w = \x + \w - 1
    h = \h - 1
    For y = \y To h
      For x = \x To w
        Plot(x,y,\c)
      Next
    Next
    ProcedureReturn #Null
  EndWith
EndProcedure  

Procedure.i Main()
  Protected exit.i
  Protected w.i
  Protected h.i
  Protected f.i
  Protected t.q
  Protected *stp.STP
  If stpVersion() = #STP_VERSION
    *stp = stpInterface()
    If *stp
      If *stp\PoolCreate(8) = 8
        If InitSprite()
          If OpenWindow(0,0,0,1920,1080,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
            w = WindowWidth(0)
            h = WindowHeight(0)
            If OpenWindowedScreen(WindowID(0),0,0,w,h)
              SetFrameRate(60)
              DrawSet(task\draw[0],0,0,w,h,RGB(72,200,100))
              w >> 3
              For f = 1 To 8
                DrawSet(task\draw[f],((f - 1) * w),0,w,h,RGB(f * 72,200,f * 100))
                *stp\PoolTaskAdd(@Draw(),@task\draw[f])
              Next
              f = #False;<- if #True use threads
              Repeat
                Repeat
                  Select WindowEvent()
                    Case #PB_Event_CloseWindow
                      exit = #True
                    Case #PB_Event_None
                      Break
                  EndSelect
                ForEver
                ClearScreen($0)
                If StartDrawing(ScreenOutput())
                  t = ElapsedMilliseconds()
                  If f
                    *stp\PoolExecuteWait()
                  Else
                    Draw(@task\draw[0])
                  EndIf
                  t = ElapsedMilliseconds() - t
                  DrawText(16,16,"MS: " + Str(t),#White)
                  StopDrawing()
                EndIf
                FlipBuffers()
              Until exit
              CloseScreen()
            EndIf
            CloseWindow(0)  
          EndIf  
        EndIf
      EndIf
      *stp\Release()
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
Have fun
Fred
Administrator
Administrator
Posts: 16616
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Windows x64] Simple Thread Pool Library

Post by Fred »

Thanks for sharing
Post Reply