|
| 1 | +'' examples/manual/proguide/multithreading/criticalsectionfaq13-1.bas |
| 2 | +'' |
| 3 | +'' Example extracted from the FreeBASIC Manual |
| 4 | +'' from topic 'Critical Sections FAQ' |
| 5 | +'' |
| 6 | +'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgMtCriticalSectionsFAQ |
| 7 | +'' -------- |
| 8 | + |
| 9 | +Dim Shared As Any Ptr mutex0, mutex1, mutex2, mutex, cond1, cond2, pt |
| 10 | +Dim Shared As Integer flag1, flag2 |
| 11 | +Dim As Double t |
| 12 | + |
| 13 | +'---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +#if defined(__FB_WIN32__) |
| 16 | +Declare Function _setTimer Lib "winmm" Alias "timeBeginPeriod"(ByVal As ULong = 1) As Long |
| 17 | +Declare Function _resetTimer Lib "winmm" Alias "timeEndPeriod"(ByVal As ULong = 1) As Long |
| 18 | +#endif |
| 19 | + |
| 20 | +Sub ThreadFlag(ByVal p As Any Ptr) |
| 21 | + MutexUnlock(mutex0) '' unlock mutex for main thread |
| 22 | + For I As Integer = 1 To 100 |
| 23 | + While flag1 = 0 |
| 24 | + Sleep 1, 1 |
| 25 | + Wend |
| 26 | + flag1 = 0 |
| 27 | + ' only child thread code runs (location for example) |
| 28 | + flag2 = 1 |
| 29 | + Next I |
| 30 | +End Sub |
| 31 | + |
| 32 | +mutex0 = MutexCreate() |
| 33 | +MutexLock(mutex0) |
| 34 | + |
| 35 | +pt = ThreadCreate(@ThreadFlag) |
| 36 | +MutexLock(mutex0) '' wait for thread launch (mutex unlock from child thread) |
| 37 | +Print "Thread synchronization latency by simple flags:" |
| 38 | +#if defined(__FB_WIN32__) |
| 39 | + _setTimer() |
| 40 | + Print "(in high resolution OS cycle period)" |
| 41 | +#else |
| 42 | + Print "(in normal resolution OS cycle period)" |
| 43 | +#endif |
| 44 | +t = Timer |
| 45 | +For I As Integer = 1 To 100 |
| 46 | + flag1 = 1 |
| 47 | + While flag2 = 0 |
| 48 | + Sleep 1, 1 |
| 49 | + Wend |
| 50 | + flag2 = 0 |
| 51 | + ' only main thread code runs (location for example) |
| 52 | +Next I |
| 53 | +t = Timer - t |
| 54 | +#if defined(__FB_WIN32__) |
| 55 | + _resetTimer() |
| 56 | +#endif |
| 57 | +ThreadWait(pt) |
| 58 | +Print Using "####.## milliseconds per double synchronization (round trip)"; t * 10 |
| 59 | +Print |
| 60 | + |
| 61 | +MutexDestroy(mutex0) |
| 62 | + |
| 63 | +'---------------------------------------------------------------------------------- |
| 64 | + |
| 65 | +Sub ThreadMutex(ByVal p As Any Ptr) |
| 66 | + MutexUnlock(mutex0) '' unlock mutex for main thread |
| 67 | + For I As Integer = 1 To 100000 |
| 68 | + MutexLock(mutex1) '' wait for mutex unlock from main thread |
| 69 | + ' only child thread code runs |
| 70 | + MutexUnlock(mutex2) '' unlock mutex for main thread |
| 71 | + Next I |
| 72 | +End Sub |
| 73 | + |
| 74 | +mutex0 = MutexCreate() |
| 75 | +mutex1 = MutexCreate() |
| 76 | +mutex2 = MutexCreate() |
| 77 | +MutexLock(mutex0) |
| 78 | +MutexLock(mutex1) |
| 79 | +MutexLock(mutex2) |
| 80 | + |
| 81 | +pt = ThreadCreate(@ThreadMutex) |
| 82 | +MutexLock(mutex0) '' wait for thread launch (mutex unlock from child thread) |
| 83 | +Print "Thread synchronization latency by mutual exclusions:" |
| 84 | +t = Timer |
| 85 | +For I As Integer = 1 To 100000 |
| 86 | + MutexUnlock(mutex1) '' mutex unlock for child thread |
| 87 | + MutexLock(mutex2) '' wait for mutex unlock from child thread |
| 88 | + ' only main thread code runs |
| 89 | +Next I |
| 90 | +t = Timer - t |
| 91 | +ThreadWait(pt) |
| 92 | +Print Using "####.## microseconds per double synchronization (round trip)"; t * 10 |
| 93 | +Print |
| 94 | + |
| 95 | +MutexDestroy(mutex0) |
| 96 | +MutexDestroy(mutex1) |
| 97 | +MutexDestroy(mutex2) |
| 98 | + |
| 99 | +'---------------------------------------------------------------------------------- |
| 100 | + |
| 101 | +Sub ThreadCondVar(ByVal p As Any Ptr) |
| 102 | + MutexUnlock(mutex0) '' unlock mutex for main thread |
| 103 | + For I As Integer = 1 To 100000 |
| 104 | + MutexLock(mutex) |
| 105 | + While flag1 = 0 |
| 106 | + CondWait(cond1, mutex) '' wait for conditional signal from main thread |
| 107 | + Wend |
| 108 | + flag1 = 0 |
| 109 | + ' only child thread code runs (location for example) |
| 110 | + flag2 = 1 |
| 111 | + CondSignal(cond2) '' send conditional signal to main thread |
| 112 | + MutexUnlock(mutex) |
| 113 | + Next I |
| 114 | +End Sub |
| 115 | + |
| 116 | +mutex0 = MutexCreate() |
| 117 | +mutex = MutexCreate() |
| 118 | +MutexLock(mutex0) |
| 119 | +cond1 = CondCreate() |
| 120 | +cond2 = CondCreate() |
| 121 | + |
| 122 | +pt = ThreadCreate(@ThreadCondVar) |
| 123 | +MutexLock(mutex0) '' wait for thread launch (mutex unlock from child thread) |
| 124 | +Print "Thread synchronization latency by conditional variables:" |
| 125 | +t = Timer |
| 126 | +For I As Integer = 1 To 100000 |
| 127 | + MutexLock(mutex) |
| 128 | + flag1 = 1 |
| 129 | + CondSignal(cond1) '' send conditional signal to main thread |
| 130 | + While flag2 = 0 |
| 131 | + CondWait(Cond2, mutex) '' wait for conditional signal from child thread |
| 132 | + Wend |
| 133 | + flag2 = 0 |
| 134 | + ' only child thread code runs (location for example) |
| 135 | + MutexUnlock(mutex) |
| 136 | +Next I |
| 137 | +t = Timer - t |
| 138 | +ThreadWait(pt) |
| 139 | +Print Using "####.## microseconds per double synchronization (round trip)"; t * 10 |
| 140 | +Print |
| 141 | + |
| 142 | +MutexDestroy(mutex0) |
| 143 | +MutexDestroy(mutex) |
| 144 | +CondDestroy(cond1) |
| 145 | +CondDestroy(cond2) |
| 146 | + |
| 147 | +'---------------------------------------------------------------------------------- |
| 148 | + |
| 149 | +Sleep |
0 commit comments