Platforms to show: All Mac Windows Linux Cross-Platform
Last modified Fri, 21th May 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Util/AtomicDictionary test
Download this example: AtomicDictionary test.zip
Project "AtomicDictionary test.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control TypeRadioGroup Inherits DesktopRadioGroup
ControlInstance TypeRadioGroup Inherits DesktopRadioGroup
End Control
Control ThreadRadioGroup Inherits DesktopRadioGroup
ControlInstance ThreadRadioGroup Inherits DesktopRadioGroup
End Control
Control StartButton Inherits DesktopButton
ControlInstance StartButton Inherits DesktopButton
EventHandler Sub Pressed()
Var UseAtomic As Boolean = TypeRadioGroup.SelectedIndex = 1
var UseCooperative as Boolean = ThreadRadioGroup.SelectedIndex = 0
dic = New Dictionary
atomicDic = New AtomicDictionaryMBS
If UseAtomic = False And UseCooperative = False Then
MessageBox "This combination is likely to crash!"
End If
For i As Integer = 1 To 4
Var m As New myThread
If UseCooperative Then
m.Type = thread.Types.Cooperative
Else
m.Type = thread.Types.Preemptive
End If
Threads.Add m
m.index = Threads.Count
m.UseAtomic = UseAtomic
m.dic = dic
m.AtomicDic = atomicDic
m.Start
Next
Running = True
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
CheckFinished
End EventHandler
End Control
EventHandler Sub Opening()
End EventHandler
Sub AddMessage(m as string)
list.AddRow m
End Sub
Sub CheckFinished()
If Running Then
if myThread.StartCount = myThread.FinishedCount then
DidFinish
End If
end If
End Sub
Sub DidFinish()
list.AddRow "All finished"
Running = False
Var sum As Int64
If dic.Count > 0 Then
// Standard Dictionary
For Each d As DictionaryEntry In dic
sum = sum + d.Value.IntegerValue
Next
Else
// Atomic Dictionary
For Each d As AtomicDictionaryEntryMBS In AtomicDic
sum = sum + d.Value.IntegerValue
Next
End If
list.AddRow "Sum: "+sum.ToString
End Sub
Property Running As Boolean
Property atomicDic As AtomicDictionaryMBS
Property dic As Dictionary
Property Private threads() As myThread
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
Class myThread Inherits Thread
EventHandler Sub Run()
SendMessage "Thread "+index.ToString+" launched."
StartCount = StartCount + 1
var StartTime as Double = System.Microseconds
If UseAtomic Then
runAtomic
else
runStandard
End If
Var EndTime As Double = System.Microseconds
Var Delta As Double = (EndTime-StartTime)/1000000.0
SendMessage "Thread "+ index.ToString+" finished after "+str(delta, "0.00")+" seconds."
FinishedCount = FinishedCount + 1
End EventHandler
EventHandler Sub UserInterfaceUpdate(data() as Dictionary)
For Each d As Dictionary In data
Var m As String = d.Value("message")
MainWindow.AddMessage m
next
End EventHandler
Sub SendMessage(message as string)
Var dic As New Dictionary
dic.Value("message") = message
Me.AddUserInterfaceUpdate dic
End Sub
Private Sub runAtomic()
Var r As New random
For i As Integer = 1 To 1000
Var index As Integer = r.InRange(0, 1000)
#if false
// 1: This may skip an increment if other thread writes first
atomicDic.Value(index) = atomicDic.Lookup(index, 0) + 1
#elseif false
// 2: we check the value and increase it. If someone else is quicker, we don't set it.
Var okay As Boolean
Do
var oldValue as Integer = atomicDic.Lookup(index, 0)
Var newValue As Integer = oldValue + 1
okay = AtomicDic.CompareAndSet(index, oldValue, newValue)
If Not okay Then
System.DebugLog "Other thread was quicker. Trying again!"
end if
loop until okay
#Else
// 3: this does the increment in one call
Call AtomicDic.Increment(index)
#endif
me.Sleep(1)
Next
End Sub
Private Sub runStandard()
Var r As New random
For i As Integer = 1 To 1000
Var index As Integer = r.InRange(0, 1000)
dic.Value(index) = dic.Lookup(index, 0) + 1
Me.Sleep(1)
Next
End Sub
Property AtomicDic As AtomicDictionaryMBS
Property Shared FinishedCount As Integer
Property Shared StartCount As Integer
Property UseAtomic As Boolean
Property dic As Dictionary
Property index As Integer
End Class
End Project
Download this example: AtomicDictionary test.zip
The items on this page are in the following plugins: MBS Util Plugin.