Chronology for Thread#1 signaling while Thread#0 is waiting: Thread#0 locks the mutex Thread#0 executes pre-code with exclusion Thread#0 is waiting Thread#1 locks the mutex Thread#1 executes code with exclusion Thread#1 is signaling Thread#1 executes post-code with exclusion Thread#1 unlocks the mutex Thread#0 is waked Thread#0 executes code with exclusion Thread#0 unlocks the mutex Thread#1 finished
Chronology for Thread#1 signaling before Thread#0 is waiting: Thread#1 locks the mutex Thread#1 executes code with exclusion Thread#1 is signaling Thread#1 executes post-code with exclusion Thread#1 unlocks the mutex Thread#0 locks the mutex Thread#0 executes pre-code with exclusion Thread#0 executes code with exclusion Thread#0 unlocks the mutex Thread#1 finished
' Thread#0 XOR + <==> Thread#1 ' ..... ..... ' MutexLock(mut) <-------------------------. .----------> MutexLock(mut) ' ( atomic_mutex_unlock(mut) ) ------. | | Do_something_with_exclusion ' CondWait(cond#1, mut) <----------- | --- | ----------- | ---------- CondSignal(cond#1) ' ( atomic_mutex_re-lock(mut) ) <--- | ----'----. | .----- ( atomic_mutex_unlock(mut) ) ' Do_something_with_exclusion | .--- | ------ | --- | ---> CondWait(cond#2, mut) ' CondSignal(cond#2) --------------- | ----' | .---'---- | ---> ( atomic_mutex_re-lock(mut) ) ' Do_something_with_exclusion | .--- | ---' | Do_something_with_exclusion ' MutexUnlock(mut) ------------------'-----' '--------------'----- MutexUnlock(mut) ' ..... ..... ' ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence)
' Thread#0 XOR + <==> Thread#1 ' ..... ..... ' MutexLock(mut) <-----------------------------. .----> MutexLock(mut) ' While bool#1 <> true <---------------------- | --------. | Do_something_with_exclusion ' ( atomic_mutex_unlock(mut) ) ------. | '--------------- | ---- bool#1 = true ' CondWait(cond#1, mut) <----------- | --- | ------------------------ | ---- CondSignal(cond#1) ' ( atomic_mutex_re-lock(mut) ) <--- | ----'----. .--------------- | ---> While bool#2 <> true ' Wend | | | .---- | -------- ( atomic_mutex_unlock(mut) ) ' bool#1 = false .------- | -------- | ---' .------ | --- |--------> CondWait(cond#2, mut) ' Do_something_with_exclusion | .--- | -------- | ------' .--- | ----'--------> ( atomic_mutex_re-lock(mut) ) ' bool#2 = true ----------------' | | .--- | ---------' | Wend ' CondSignal(cond#2) ---------------' | | | | bool#2 = false ' Do_something_with_exclusion | | | | Do_something_with_exclusion ' MutexUnlock(mut) ----------------------'-----' '---------------'----------- MutexUnlock(mut) ' ..... ..... ' ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence)
' Principle of mutual exclusion + CONDWAIT in a While...Wend loop with predicate check, for a thread sub-section ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence) ' ' ' Thread Other Thread ' MUTEXLOCK(mutexID) <------------------------- from ( atomic_mutex_unlock(mutexID) ) or MUTEXUNLOCK(mutexID) ' ....... ' While booleanT <> True <--------------------- from booleanT = True ' ( atomic_mutex_unlock(mutexID) ) -------> to MUTEXLOCK(mutexID) or ( atomic_mutex_re-lock(mutexID) ) ' CONDWAIT(conditionalID, mutexID) <------- from CONDSIGNAL(conditionalID) ' ( atomic_mutex_re-lock(mutexID) ) <------ from ( atomic_mutex_unlock(mutexID) ) or MUTEXUNLOCK(mutexID) ' Wend ' booleanT = False ' ....... ' MUTEXUNLOCK(mutexID) -----------------------> to MUTEXLOCK(mutexID) or ( atomic_mutex_re-lock(mutexID) )
' Principle of mutual exclusion + CONDSIGNAL with predicate check, for a thread sub-section ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence) ' ' Thread Other Thread ' MUTEXLOCK(mutexID) <------------------------- from ( atomic_mutex_unlock(mutexID) ) or MUTEXUNLOCK(mutexID) ' ....... ' booleanOT = True ---------------------------> to While booleanOT <> True ' CONDSIGNAL(conditionalID) ------------------> to CONDWAIT(conditionalID, mutexID) ' ....... ' MUTEXUNLOCK(mutexID) -----------------------> to MUTEXLOCK(mutexID) or ( atomic_mutex_re-lock(mutexID) )
' Principle of mutual exclusion + SLEEP in a While...Wend loop with predicate check, for a thread sub-section ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence) ' ' Thread Other Thread ' MUTEXLOCK(mutexID) <------------------------- from MUTEXUNLOCK(mutexID) ' ....... ' While booleanT <> True <--------------------- from booleanT = True ' MUTEXUNLOCK(mutexID) -------------------> to MUTEXLOCK(mutexID) ' SLEEP(tempo, 1) ' MUTEXLOCK(mutexID) <--------------------- from MUTEXUNLOCK(mutexID) ' Wend ' booleanT = False ' ....... ' MUTEXUNLOCK(mutexID) -----------------------> to MUTEXLOCK(mutexID)
' Principle of mutual exclusion + predicate check only, for a thread sub-section ' (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence) ' ' Thread Other Thread ' MUTEXLOCK(mutexID) <------------------------- from MUTEXUNLOCK(mutexID) ' ....... ' booleanOT = True ---------------------------> to While booleanOT <> True ' ....... ' MUTEXUNLOCK(mutexID) -----------------------> to MUTEXLOCK(mutexID)
' User thread algorithm: ' ' Do ' | Mutexlock ' | ..... ' | Critical section of code ' | ..... ' | Mutexunlock ' | Sleep my_tempo, 1 ' Loop Until quit = true ' ' There is no any advantage or disadvantage between threads for running their critical sections.
Do ' instructions without display (printing/drawing, ...) neither input (input/inkey/mouse getting, ...) MutexLock(m) Screenlock ' instructions with only display (printing/drawing, ...) Screenunlock ' instructions with only input without waiting (inkey/mouse getting, ...) MutexUnlock(m) Sleep tempo, 1 Loop Until condition
Tmin/Tmean/Tmax between begin of thread code and return from ThreadCreate() : 21:30:13 Tmin= +0.151800 ms Tmean= +0.151800 ms Tmax= +0.151800 ms 21:30:13 Tmin= +0.006000 ms Tmean= +0.078900 ms Tmax= +0.151800 ms 21:30:13 Tmin= +0.006000 ms Tmean= +0.098394 ms Tmax= +0.172500 ms 21:30:13 Tmin= +0.006000 ms Tmean= +0.121555 ms Tmax= +0.884900 ms 21:30:45 Tmin= +0.006000 ms Tmean= +0.055810 ms Tmax= +1.104200 ms 21:30:54 Tmin= +0.006000 ms Tmean= +0.055764 ms Tmax= +4.056600 ms 21:31:44 Tmin= -0.116300 ms Tmean= +0.055516 ms Tmax= +4.056600 ms 21:32:10 Tmin= -0.136800 ms Tmean= +0.057177 ms Tmax= +4.056600 ms 21:32:12 Tmin= -0.150300 ms Tmean= +0.057265 ms Tmax= +4.056600 ms 21:33:17 Tmin= -0.150300 ms Tmean= +0.060048 ms Tmax= +4.979900 ms 21:33:18 Tmin= -0.150300 ms Tmean= +0.060157 ms Tmax= +7.086300 ms 21:33:23 Tmin= -0.150600 ms Tmean= +0.060347 ms Tmax= +7.086300 ms 21:33:38 Tmin= -0.205900 ms Tmean= +0.060878 ms Tmax= +7.086300 ms 21:35:30 Tmin= -0.208700 ms Tmean= +0.061315 ms Tmax= +7.086300 ms