Skip to content

Commit ff0e6ed

Browse files
committed
fbdocs: wiki snapshot 2025.04.26 - update examples/manual
1 parent d166021 commit ff0e6ed

13 files changed

+768
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'' examples/manual/defines/fbarglistexpand.bas
2+
''
3+
'' Example extracted from the FreeBASIC Manual
4+
'' from topic '__FB_ARG_LISTEXPAND__'
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbarglistexpand
7+
'' --------
8+
9+
#macro m( arg... )
10+
#print " "##arg
11+
#endmacro
12+
13+
#print "macroargcount>0 (=1):"
14+
__FB_ARG_LISTEXPAND__( m, 1, Hello1, Hello2, Hello3, Hello4)
15+
#print " "
16+
#print "macroargcount=0 (=0):"
17+
__FB_ARG_LISTEXPAND__( m, 0, Hello1, Hello2, Hello3, Hello4)
18+
#print " "
19+
#print "macroargcount<0 (=-1):"
20+
__FB_ARG_LISTEXPAND__( m, -1, Hello1, Hello2, Hello3, Hello4)
21+
22+
23+
/' Compiler output:
24+
macroargcount>0 (=1):
25+
Hello1
26+
Hello2
27+
Hello3
28+
Hello4
29+
30+
macroargcount=0 (=0):
31+
Hello1, Hello2, Hello3, Hello4
32+
33+
macroargcount<0 (=-1):
34+
Hello1, Hello2, Hello3, Hello4
35+
Hello2, Hello3, Hello4
36+
Hello3, Hello4
37+
Hello4
38+
'/

examples/manual/defines/fberr.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ Else
4343
If fb_err_value And 512 Then
4444
Print "'unwindinfo' flag enabled"
4545
End If
46+
If fb_err_value And 1024 Then
47+
Print "'arraydimscheck' flag enabled"
48+
End If
4649
End If

examples/manual/defines/fbquerysymbol3.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ End Namespace
1919

2020
Print "type name : " & __FB_QUOTE__( __FB_QUERY_SYMBOL__( FB_QUERY_SYMBOL.typename, NS.T.pps ) )
2121
Print "type name id : " & __FB_QUOTE__( __FB_QUERY_SYMBOL__( FB_QUERY_SYMBOL.typenameid, NS.T.pps ) )
22-
Print "mangled id : " & __FB_QUOTE__( __FB_QUERY_SYMBOL__( FB_QUERY_SYMBOL.mangleid, NS.T.pps ) )
22+
Print "mangled type : " & __FB_QUOTE__( __FB_QUERY_SYMBOL__( FB_QUERY_SYMBOL.mangletype, NS.T.pps ) )
2323

2424
Sleep
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'' examples/manual/proguide/binaries/simple-fb-profiling-cycles.bas
2+
''
3+
'' Example extracted from the FreeBASIC Manual
4+
'' from topic 'Profiling with fb's profiler'
5+
''
6+
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgProfilingFbProfiler
7+
'' --------
8+
9+
#cmdline "-gen gas64"
10+
#cmdline "-profgen cycles"
11+
12+
Sub A()
13+
Dim As Integer I
14+
For J As Integer = 1 To 1000
15+
I = I + 1
16+
Next J
17+
End Sub
18+
19+
Sub B()
20+
A()
21+
End Sub
22+
23+
Sub C()
24+
B()
25+
Print "End"
26+
End Sub
27+
28+
C()
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'' examples/manual/proguide/multithreading/criticalsectionfaq13-2.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 pt, mutex1, mutex2, cond1, cond2
10+
Dim Shared As Integer quit, flag1, flag2
11+
12+
Print "'1': Main thread procedure running (alone)"
13+
Print "'2': Child thread procedure running (alone)"
14+
Print "'-': Main thread procedure running (with the one of child thread)"
15+
Print "'=': Child thread procedure running (with the one of main thread)"
16+
Print
17+
18+
Sub Prnt(ByRef s As String, ByVal n As Integer)
19+
For I As Integer = 1 To n
20+
Print s;
21+
Sleep 20, 1
22+
Next I
23+
End Sub
24+
25+
Sub ThreadCondCond(ByVal p As Any Ptr)
26+
Do
27+
MutexLock(mutex1)
28+
While flag1 = 0 '' test flag set from main thread
29+
CondWait(cond1, mutex1) '' wait for conditional signal from main thread
30+
Wend
31+
flag1 = 0 '' reset flag
32+
MutexUnlock(mutex1)
33+
If quit = 1 Then Exit Sub '' exit the threading loop
34+
Prnt("=", 10)
35+
MutexLock(mutex2)
36+
flag2 = 1 '' set flag to main thread
37+
CondSignal(cond2) '' send conditional signal to main thread
38+
Prnt("2", 10)
39+
MutexUnlock(mutex2)
40+
Loop
41+
End Sub
42+
43+
mutex1 = MutexCreate()
44+
mutex2 = MutexCreate()
45+
cond1 = CondCreate()
46+
cond2 = CondCreate()
47+
48+
pt = ThreadCreate(@ThreadCondCond)
49+
For I As Integer = 1 To 5
50+
MutexLock(mutex1)
51+
flag1 = 1 '' set flag to child thread
52+
CondSignal(cond1) '' send conditional signal to child thread
53+
MutexUnlock(mutex1)
54+
Prnt("-", 10)
55+
MutexLock(mutex2)
56+
While flag2 = 0 '' test flag set from child thread
57+
CondWait(Cond2, mutex2) '' wait for conditional signal from child thread
58+
Wend
59+
flag2 = 0 '' reset flag
60+
Prnt("1", 10)
61+
MutexUnlock(mutex2)
62+
Next I
63+
64+
MutexLock(mutex1)
65+
quit = 1 '' set quit for child thread
66+
flag1 = 1
67+
CondSignal(cond1) '' send conditional signal to child thread
68+
MutexUnlock(mutex1)
69+
ThreadWait(pt) '' wait for child thread to end
70+
Print
71+
72+
MutexDestroy(mutex1)
73+
MutexDestroy(mutex2)
74+
CondDestroy(cond1)
75+
CondDestroy(cond2)
76+
77+
Sleep

0 commit comments

Comments
 (0)