Thread and gadget creation
Re: Thread and gadget creation
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Thread and gadget creation
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
Thanks for your example.infratec wrote: With Semaphore:
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
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...
Except on this sentence...
Re: Thread and gadget creation
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.
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
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Thread and gadget creation
How simple words can lead to great understanding!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.
One small step for you, ONE GIANT LEAP FOR ME !!!!
Thanks a lot ! I'll try to assimilate all thismk-soft wrote: Sat Jan 20, 2024 2:07 pm For working with threads see Mini Thread Control with examples
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...
Except on this sentence...
Re: Thread and gadget creation
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.
I cannot signal multiple threads with a mutex.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Thread and gadget creation
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
Semaphores can signal other semaphores.
My main loop (gui) never attempts to touch the threaded data until a thread posts an event.
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
Re: Thread and gadget creation
I agree, it's almost clearer written like this than the full detailed description of the help file.boddhi wrote: Sat Jan 20, 2024 3:07 pmHow simple words can lead to great understanding!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.![]()
One small step for you, ONE GIANT LEAP FOR ME !!!!![]()
![]()
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
Je dirais même plus ! C'est vachement plus clair écrit comme ça !!!ChrisR wrote: It's almost clearer written like this than the full detailed description of the help file.
(For non-French speakers, i'ts a reference to Tintin and Thomson and Thompson
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...
Except on this sentence...


