Is this thread safe?

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Is this thread safe?

Post by Randy Walker »

I modified the sample code to have the tread kill itself. Is that a bad thing?

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Thread example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;
Global thread
Procedure AlertThread(Parameter)
  
  Repeat
    Debug "Alert ! "+Str(Parameter)
    Delay(3000)
    Parameter-1
    If Parameter = 147
      KillThread(thread)
    EndIf
  ForEver
  
EndProcedure

thread = CreateThread(@AlertThread(), 154)

; MessageRequester("Info", "It will display an alert every 3 seconds."+#LF$+"Click To kill the alerts", 0) 
; KillThread(thread)
MessageRequester("Info", "Click To finish the program", 0) 
I am considering use of threads, but I want the thread to die after it completes it's task.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is this thread safe?

Post by mk-soft »

Very Bad.

KillThread should only be used in extreme cases of errors and can lead to memory leaks.

Better ...

Code: Select all

EnableExplicit

Global thread
Global ExitThread

Procedure AlertThread(Parameter)
  ; Init resources
  
  ; Loop Thread
  Repeat
    Debug "Alert ! "+Str(Parameter)
    Delay(3000)
    Parameter-1
    If Parameter = 147
      Break
    EndIf
  Until ExitThread
  ; Release Thread resources
  
  ; Exit Thread
EndProcedure

thread = CreateThread(@AlertThread(), 154)

; MessageRequester("Info", "It will display an alert every 3 seconds."+#LF$+"Click To kill the alerts", 0) 
; KillThread(thread)
MessageRequester("Info", "Click To finish the program", 0) 

ExitThread = #True
If WaitThread(thread, 5000) = 0
  Debug "Thread hangs. KillThread"
  KillThread(Thread)
Else
  Debug "Thread Done."
EndIf
Last edited by mk-soft on Sat Apr 12, 2025 10:56 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Is this thread safe?

Post by HeX0R »

A thread never has to "kill" itself, you simply let it end.
Replace KillThread(thread) with break and all is fine.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is this thread safe?

Post by mk-soft »

And with structured data ...

Code: Select all


EnableExplicit

Structure structData
  ThreadID.i
  Exit.i
  Count.i
EndStructure

Global thData.structData

Procedure AlertThread(*Data.structData)
  With *Data
    ; Init resources
    
    ; Loop Thread
    Repeat
      Debug "Alert ! "+Str(\Count)
      Delay(3000)
      \Count-1
      If \Count = 147
        Break
      EndIf
    Until \Exit
    ; Release Thread resources
    
    ; Exit Thread
  EndWith
EndProcedure

thData\Count = 154
thData\ThreadID = CreateThread(@AlertThread(), @thData)

; MessageRequester("Info", "It will display an alert every 3 seconds."+#LF$+"Click To kill the alerts", 0) 
; KillThread(thread)
MessageRequester("Info", "Click To finish the program", 0) 

thData\Exit = #True
If WaitThread(thData\ThreadID, 5000) = 0
  Debug "Thread hangs. KillThread"
  KillThread(thData\ThreadID)
Else
  Debug "Thread Done."
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply