Platforms to show: All Mac Windows Linux Cross-Platform

/Util/AtomicInteger test


Required plugins for this example: MBS Util Plugin

Last modified Mon, 20th Apr 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Util/AtomicInteger test

Download this example: AtomicInteger test.zip

Project "AtomicInteger test.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
EventHandler Sub Opening() GlobalCounter = New AtomicIntegerMBS For i As Integer = 1 To 10 StartThread i Next // runs for a while // Last ID must be 10 * 20000 = 200000 End EventHandler
Sub StartThread(ID as integer) Var m As New MyThread m.ID = ID m.Type = Thread.Types.Preemptive m.Run Threads.add m End Sub
Property 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() For i As Integer = 1 To 20000 Test Next // finished Var d As New Dictionary Me.AddUserInterfaceUpdate d End EventHandler
EventHandler Sub UserInterfaceUpdate(data() as Dictionary) MainWindow.List.AddRow Me.ID.toString, Me.lastID.ToString, Me.Misses.ToString End EventHandler
Sub Test() // try to get new unique ID Do Var Expected As Int64 = GlobalCounter Var newCount As Int64 = Expected+1 Var r As Boolean = GlobalCounter.CompareExchange(Expected, newCount) If r Then // We got our new ID, so continue lastID = newCount Exit Else // some other thread was quicker, so try again! Misses = Misses + 1 Continue End If Loop End Sub
Property ID As Integer
Property Misses As Integer
Property lastID As Integer
End Class
Module Module1
Property GlobalCounter As AtomicIntegerMBS
End Module
End Project

Download this example: AtomicInteger test.zip

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...