Page 1 of 1

Threads Library

Posted: Tue Jun 27, 2006 1:58 pm
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.

Posted: Tue Jun 27, 2006 3:08 pm
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 ;)

Posted: Tue Jun 27, 2006 6:16 pm
by Flype
Both are interessant. thank you guys.

Macro seems to be a robust way to PATCH purebasic built-in commands.