Page 2 of 2

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 7:43 am
by jacdelad
Yeah, something like that.

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 10:35 am
by infratec
With Semaphore:

Code: Select all

Structure Thread_Structure
  Thread.i
  Semaphore.i
  Exit.i
EndStructure


Procedure CalledProcedure(*buffer.structureX)
  
EndProcedure

Procedure ThreadedProcedure(*Parameter.Thread_Structure)
  
  Repeat
    
    PostEvent(#EVNMT)
    WaitSemaphore(*Parameter\Semaphore)
    
  Until *Parameter\Exit  
  
EndProcedure


Define Parameter.Thread_Structure

Parameter\Semaphore = CreateSemaphore()
Parameter\Thread = CreateThread(@ThreadedProcedure(), @Parameter)

Repeat
  Select WaitWindowEvent()
    [...]
  Case #EVNMT
    CalledProcedure()
    SignalSemaphore(Parameter\Semaphore)
    [...]
  EndSelect
Until Exit

If IsThread(Parameter\Thread)
  Parameter\Exit = #True
  If WaitThread(Parameter\Thread, 3000) = 0
    Debug "Should never happen"
    KillThread(Parameter\Thread)
  EndIf
EndIf

FreeSemaphore(Parameter\Semaphore)

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 1:44 pm
by boddhi
infratec wrote: With Semaphore:
Thanks for your example.

I already use a mutex every time I open a threaded procedure.
Won't adding a semaphore create a problem?
I'm really having trouble understanding how threads work :oops: :mrgreen: !

Here's how I build my thread :

Code: Select all

Structure INFOSTHREAD
  IDThread.i
  Mutex.i
  Demarrage.a ; Start
  Pause.a 
  Arret.a ; Stop
EndStructure
Structure DONNEESTHREAD Extends INFOSTHREAD ; Various variables required by the calling procedure
  ; Data
  ModeRecursif.a
  NiveauRecursif.i
  CheminOriginel.s
  Chemin.s
  ;Filtre.s
  ;Initialisation.a
  TypeTraitement.a
  NbDossiers.l
  NbFichiers.l
EndStructure

Procedure   Pc_Initialisation_ThreadAnalyseDossiers()
  Global *DonneesThread.DONNEESTHREAD=AllocateStructure(DONNEESTHREAD)
EndProcedure

Procedure.i Fc_Thread_Demarrage(*ArgThread.INFOSTHREAD,*ArgProcedure)
  With *ArgThread
    If Not IsThread(\IDThread)
      \Demarrage=#True
      \Pause=#False
      \Arret=#False
      \IDThread=CreateThread(*ArgProcedure,*ArgThread)
    EndIf
    ProcedureReturn \IDThread
  EndWith
EndProcedure
Procedure   Pc_Thread_Pause(*ArgThread.INFOSTHREAD)
  With *ArgThread
    If IsThread(\IDThread)
      If Not \Pause
        PauseThread(\IDThread)
        \Pause=#True
      EndIf
    EndIf
  EndWith
EndProcedure
Procedure   Pc_Thread_Reprise(*ArgThread.INFOSTHREAD)
  With *ArgThread
    If IsThread(\IDThread)
      If \Pause
        ResumeThread(\IDThread)
        \Pause=#False
      EndIf
    EndIf
  EndWith
EndProcedure
Procedure   Pc_Thread_Arret(*ArgThread.INFOSTHREAD)
  With *ArgThread
    If IsThread(\IDThread)
      \Arret=#True
      If \Pause
        \Pause=#False
      EndIf
    EndIf
  EndWith
EndProcedure
Procedure   Pc_Thread_Liberation(*ArgThread.INFOSTHREAD)
  With *ArgThread
    \Pause=#False
    \Arret=#False
    \Demarrage=#False
    UnlockMutex(\Mutex)
    FreeMutex(\Mutex)
    If IsThread(\IDThread)
      KillThread(\IDThread)
    EndIf
  EndWith
EndProcedure

Procedure   ThreadedProcedure(*ArgDonneesThread.DONNEESTHREAD)
  With *ArgDonneesThread
    If \Demarrage
      \Demarrage=#False
      \Mutex=CreateMutex()
    EndIf
  EndWith
  [...]
EndProcedure

Pc_Initialisation_ThreadAnalyseDossiers()
ThreadedProcedure(*DonneesThread.DONNEESTHREAD)

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 2:05 pm
by infratec
Mutex and semaphore are completely different.

A mutex gives you exclusive access to something, because it blocks other access.
You lock it, you unlock it

A semaphore is for waiting until some other gives you a go.

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 2:07 pm
by mk-soft
For working with threads see Mini Thread Control with examples

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 3:07 pm
by boddhi
infratec wrote: Mutex and semaphore are completely different.
A mutex gives you exclusive access to something, because it blocks other access.
You lock it, you unlock it
A semaphore is for waiting until some other gives you a go.
How simple words can lead to great understanding! :wink:
One small step for you, ONE GIANT LEAP FOR ME !!!! 🧠🚀 :mrgreen:
mk-soft wrote: Sat Jan 20, 2024 2:07 pm For working with threads see Mini Thread Control with examples
Thanks a lot ! I'll try to assimilate all this :wink:

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 5:30 pm
by skywalk
I prefer semaphores since they are more flexible. And I can effectively lock data with semaphores, without adding mutexes.
I cannot signal multiple threads with a mutex.

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 5:49 pm
by infratec
skywalk wrote: Sat Jan 20, 2024 5:30 pm And I can effectively lock data with semaphores, without adding mutexes.
:?: :?: :?:

How do you do this?

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 6:06 pm
by skywalk
Semaphores can signal other semaphores.
My main loop (gui) never attempts to touch the threaded data until a thread posts an event.

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 6:21 pm
by ChrisR
boddhi wrote: Sat Jan 20, 2024 3:07 pm
infratec wrote: Mutex and semaphore are completely different.
A mutex gives you exclusive access to something, because it blocks other access.
You lock it, you unlock it
A semaphore is for waiting until some other gives you a Go.
How simple words can lead to great understanding! :wink:
One small step for you, ONE GIANT LEAP FOR ME !!!! 🧠🚀 :mrgreen:
I agree, it's almost clearer written like this than the full detailed description of the help file.
I think it should be written more or less like this in the doc before the more detailed description. These are somewhat recurring questions

Re: Thread and gadget creation

Posted: Sat Jan 20, 2024 6:49 pm
by boddhi
ChrisR wrote: It's almost clearer written like this than the full detailed description of the help file.
Je dirais même plus ! C'est vachement plus clair écrit comme ça !!! :D :D :D
(For non-French speakers, i'ts a reference to Tintin and Thomson and Thompson :wink: )