Thread and gadget creation

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Thread and gadget creation

Post by jacdelad »

Yeah, something like that.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Thread and gadget creation

Post 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)
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Thread and gadget creation

Post 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)
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Thread and gadget creation

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6321
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Thread and gadget creation

Post by mk-soft »

For working with threads see Mini Thread Control with examples
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
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Thread and gadget creation

Post 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:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Thread and gadget creation

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Thread and gadget creation

Post 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?
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Thread and gadget creation

Post by skywalk »

Semaphores can signal other semaphores.
My main loop (gui) never attempts to touch the threaded data until a thread posts an event.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Thread and gadget creation

Post 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
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Thread and gadget creation

Post 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: )
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply