Re: Thread and gadget creation
Posted: Sat Jan 20, 2024 7:43 am
Yeah, something like that.
http://www.purebasic.com
https://www.purebasic.fr/english/
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)
Thanks for your example.infratec wrote: With Semaphore:
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)
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.
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
skywalk wrote: Sat Jan 20, 2024 5:30 pm And I can effectively lock data with semaphores, without adding mutexes.
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 !!!!![]()
![]()
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.