Page 1 of 1

Threads and multiple parameters....

Posted: Thu Jun 16, 2005 11:24 pm
by roachofdeath
Threads and multiple parameters.... how?

Posted: Fri Jun 17, 2005 7:57 am
by Paul Dwyer
use a single parameter as a pointer to a structure with whatever parameters you want.

Fill a structure with the parameters for the job the thread is designed to do and pass a pointer to it to the thread.

Posted: Fri Jun 17, 2005 5:22 pm
by localmotion34
the little known, seldom used function of POSTMESSAGE_()

and PEEKMESSAGE_(). that way, its possible to have threads create, access, and alter other threads.

Posted: Fri Jun 17, 2005 9:47 pm
by Flype
use a single parameter as a pointer to a structure with whatever parameters you want.

Fill a structure with the parameters for the job the thread is designed to do and pass a pointer to it to the thread.
i come here with a small example on how to 'pass'
multiple arguments to a threaded procedure.

Code: Select all

;-
;- Purpose:   PB Threads, Passing structures as argument.
;-            In order to use multiple arguments.
;-
;- Author:    Flype (2005, june)
;- Example:   3 different 'threaded' little clocks.
;-

Structure CLOCK
  gadget.l
  gmt.b
  mask1.s
  mask2.s
  delay.l
EndStructure

Procedure ClockThread(*a.CLOCK)
  
  If *a\mask2 = ""
    *a\mask2 = *a\mask1
  EndIf
  
  Repeat
    
    tick = 1 - tick
    
    If tick
      SetGadgetText(*a\gadget, FormatDate(*a\mask1, AddDate(Date(), #PB_Date_Hour, *a\gmt)))
    Else
      SetGadgetText(*a\gadget, FormatDate(*a\mask2, AddDate(Date(), #PB_Date_Hour, *a\gmt)))
    EndIf
    
    Delay(*a\delay)
    
  ForEver

EndProcedure

OpenWindow(0,0,0,260,145,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Thread")
CreateGadgetList(WindowID())
TextGadget(0,5,  5,250,40,"",#PB_Text_Center)
TextGadget(1,5, 50,250,40,"",#PB_Text_Center)
TextGadget(2,5,100,250,40,"",#PB_Text_Center)
LoadFont(0,"Arial",24,#PB_Font_Bold)
SetGadgetFont(0,FontID())
SetGadgetFont(1,FontID())
SetGadgetFont(2,FontID())

Clock0.CLOCK
Clock0\gadget = 0
Clock0\gmt    = 0
Clock0\delay  = 1000
Clock0\mask1  = "%hh:%ii:%ss"
Clock0\mask2  = "%hh:%ii %ss"
CreateThread(@ClockThread(),@Clock0)

Clock1.CLOCK
Clock1\gadget = 1
Clock1\gmt    = 1
Clock1\delay  = 1000
Clock1\mask1  = "%hhh %iim %sss"
CreateThread(@ClockThread(),@Clock1)

Clock2.CLOCK
Clock2\gadget = 2
Clock2\gmt    = 2
Clock2\delay  = 1000
Clock2\mask1  = "%hhh %iim %sss"
CreateThread(@ClockThread(),@Clock2)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Posted: Fri Jun 17, 2005 10:04 pm
by dell_jockey
thanks Flype,

a very nice demo, without any unneccesary ballast. Very useful to me. Thanks again!

Posted: Sat Jun 18, 2005 3:14 pm
by Flype
thanks, dell_jockey.

and as a second example,
it's possible to 'encapsulate' the creation of a threaded clock in a classic procedure...

Code: Select all

;-
;- Purpose:   PB Threads, Passing structures as argument.
;-            In order to use multiple arguments.
;-
;- Author:    Flype (2005, june)
;- Example:   3 different 'threaded' little clocks.
;-

Structure CLOCK
  
  threadId.l
  gadgetId.l
  
  timeGMT.b
  timeDelay.l
  timeMask1.s
  timeMask2.s
  
EndStructure

NewList Clock.CLOCK()

Procedure ClockThread(*this.CLOCK)
  
  Repeat
    
    tick = 1 - tick
    time = AddDate(Date(), #PB_Date_Hour, *this\timeGMT)
    
    If tick
      SetGadgetText(*this\gadgetId, FormatDate(*this\timeMask1, time))
    Else
      SetGadgetText(*this\gadgetId, FormatDate(*this\timeMask2, time))
    EndIf
    
    Delay(*this\timeDelay)
    
  ForEver

EndProcedure
Procedure SetClock(gadget.l,gmt.l,delay.l,mask1.s,mask2.s)
  
  If AddElement(Clock())
    
    If mask2 = ""
      mask2 = mask1
    EndIf
    
    Clock()\gadgetId  = gadget
    Clock()\timeGMT   = gmt
    Clock()\timeDelay = delay
    Clock()\timeMask1 = mask1
    Clock()\timeMask2 = mask2
    Clock()\threadId  = CreateThread(@ClockThread(),@Clock())
    
  EndIf
  
EndProcedure

OpenWindow(0,0,0,260,145,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Thread")
CreateGadgetList(WindowID())
TextGadget(0,5,  5,250,40,"",#PB_Text_Center)
TextGadget(1,5, 50,250,40,"",#PB_Text_Center)
TextGadget(2,5,100,250,40,"",#PB_Text_Center)
LoadFont(0,"Arial",24,#PB_Font_Bold)
SetGadgetFont(0,FontID())
SetGadgetFont(1,FontID())
SetGadgetFont(2,FontID())

SetClock(0,0,1000,"%hh:%ii:%ss","%hh %ii %ss")
SetClock(1,2,2000,"%hhh%iim%sss","")
SetClock(2,4,3000,"%hhh%iim%sss","")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

ForEach Clock()
  Debug Clock()\threadId
Next
note:
i make use a linked list ( i could use an array ) because the structured variables must be 'global' to be still 'alive' from/inside the thread.