Threads Library

Share your advanced PureBasic knowledge/code with the community.
Phlos
User
User
Posts: 85
Joined: Fri May 16, 2003 7:17 pm

Threads Library

Post by Phlos »

Code updated For 5.20+

Hello,

I coded a temporary fix for this bug : http://www.purebasic.fr/english/viewtopic.php?t=22171

Code: Select all

Structure _Thread
  ThreadID.q
  ThreadPB.l
EndStructure
Global NewList _Threads._Thread()
Global _Thread.q = 0

;##################################################################################################################;
Procedure CreateThreadEx(*Function, Value)
  ForEach _Threads()
    If (IsThread(_Threads()\ThreadPB) = 0)
      DeleteElement(_Threads())
    EndIf
  Next
  
  _Thread + 1
  AddElement(_Threads())
  _Threads()\ThreadID = _Thread
  _Threads()\ThreadPB = CreateThread(*Function, Value)
  
  ProcedureReturn _Thread
EndProcedure
;##################################################################################################################;
Procedure IsThreadEx(ThreadID)
  ThreadPB = 0
  ForEach _Threads()
    If (_Threads()\ThreadID = ThreadID)
      If (IsThread(_Threads()\ThreadPB))
        ThreadPB = _Threads()\ThreadPB
        Break
      EndIf
    EndIf
  Next
  
  ProcedureReturn ThreadPB
EndProcedure
;##################################################################################################################;
Procedure KillThreadEx(ThreadID)
  ThreadPB = IsThreadEx(ThreadID)
  If (ThreadPB)
    KillThread(ThreadPB)
  EndIf
  
  ProcedureReturn 0
EndProcedure
;##################################################################################################################;
Procedure PauseThreadEx(ThreadID)
  ThreadPB = IsThreadEx(ThreadID)
  If (ThreadPB)
    PauseThread(ThreadPB)
  EndIf
  
  ProcedureReturn 0
EndProcedure
;##################################################################################################################;
Procedure ResumeThreadEx(ThreadID)
  ThreadPB = IsThreadEx(ThreadID)
  If (ThreadPB)
    ResumeThread(ThreadPB)
  EndIf
  
  ProcedureReturn 0
EndProcedure
;##################################################################################################################;
Procedure ThreadPriorityEx(ThreadID, Priority)
  ThreadPB = IsThreadEx(ThreadID)
  If (ThreadPB)
    ThreadPriority(ThreadPB, Priority)
  EndIf
  
  ProcedureReturn 0
EndProcedure
;##################################################################################################################;
Procedure WaitThreadEx(ThreadID)
  ThreadPB = IsThreadEx(ThreadID)
  If (ThreadPB)
    WaitThread(ThreadPB)
  EndIf
  
  ProcedureReturn 0
EndProcedure
;##################################################################################################################;
So you should incude this file (XIncludeFile "Threads.pb") and replace all your thread functions with thoses new "Ex" :)

Bye.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Add this to your code and you don't need anymore to deal with "Ex"
(You should add the WaitThread's timeout)

Code: Select all

Macro CreateThread(Proc, Param)
  CreateThreadEx(Proc, Param)
EndMacro

Macro IsThread(Thread)
  IsThreadEx(Thread)
EndMacro

Macro KillThread(Thread)
  KillThreadEx(Thread)
EndMacro

Macro PauseThread(Thread)
  PauseThreadEx(Thread)
EndMacro

Macro ResumeThread(Thread)
  ResumeThreadEx(Thread)
EndMacro

Macro ThreadPriority(Thread, Priority)
  ThreadPriorityEx(Thread, Priority)
EndMacro

Macro WaitThread(Thread)
  WaitThreadEx(Thread)
EndMacro
Dri ;)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Both are interessant. thank you guys.

Macro seems to be a robust way to PATCH purebasic built-in commands.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply