Przeglądaj źródła

Added TFuture type.

Brucey 1 rok temu
rodzic
commit
6db24023d8
2 zmienionych plików z 85 dodań i 1 usunięć
  1. 40 0
      threads.mod/examples/future_01.bmx
  2. 45 1
      threads.mod/threads.bmx

+ 40 - 0
threads.mod/examples/future_01.bmx

@@ -0,0 +1,40 @@
+SuperStrict
+
+Framework brl.standardio
+Import brl.threads
+
+' Define a function to perform some computation in a background thread
+Function ComputeSomethingAsync:Object( data:Object )
+	Local future:TFuture<Float> = TFuture<Float>(data)
+
+	' Simulate a time-consuming computation with Delay
+	Local result:Float = 0.0
+	For Local i:Int = 0 Until 10
+		result :+ 0.1
+		Print "Computing... " + result
+		Delay(500)
+	Next
+	' Set the result in the future object
+	future.SetResult( result )
+End Function
+
+
+' Create a TFuture instance for Float
+Local future:TFuture<Float> = New TFuture<Float>
+
+' Start the computation in a background thread
+Local thread:TThread = CreateThread( ComputeSomethingAsync, future )
+
+' Simulate doing some other work in the main thread
+Print "Main thread is doing other work..."
+For Local j:Int = 0 Until 5
+	Print "Main work step " + j
+	Delay(300)
+Next
+
+' Wait for and retrieve the result from the future object
+Local result:Float = future.GetResult()
+Print "The result of the computation is: " + result
+
+' Wait for the background thread to finish
+thread.Wait()

+ 45 - 1
threads.mod/threads.bmx

@@ -6,10 +6,12 @@ bbdoc: System/Threads
 End Rem
 Module BRL.Threads
 
-ModuleInfo "Version: 1.02"
+ModuleInfo "Version: 1.03"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 
+ModuleInfo "History: 1.03"
+ModuleInfo "History: Added TFuture type."
 ModuleInfo "History: 1.02"
 ModuleInfo "History: Changed to use macOS dispatch semphores."
 ModuleInfo "History: 1.01"
@@ -363,6 +365,48 @@ Type TCondVar
 
 End Type
 
+Rem
+bbdoc: A generic type for asynchronous result handling, allowing threads to wait for and retrieve results safely.
+about: It provides a mechanism for one thread to produce a result that another thread can wait for and retrieve
+at a later time. This is particularly useful for tasks that are executed in parallel, where the completion
+time may vary, and the consumer needs to wait for the result before proceeding.
+End Rem
+Type TFuture<V>
+Private
+	Field value:V
+
+	Field ready:Int
+
+	Field condvar:TCondVar=CreateCondVar()
+	Field mutex:TMutex=CreateMutex()
+Public
+	Rem
+	bbdoc: Waits for the result to become available and then returns it.
+	about: This method blocks the calling thread until result is available.
+	End Rem
+	Method GetResult:V()
+		mutex.Lock()
+		While Not ready
+			condvar.Wait( mutex )
+		Wend
+		Local result:V = value
+		mutex.Unlock()
+		Return result
+	End Method
+
+	Rem
+	bbdoc: Sets the result of the asynchronous operation and signals any waiting threads.
+	End Rem
+	Method SetResult( value:V )
+		mutex.Lock()
+		Self.value=value
+		Self.ready=True
+		condvar.Signal()
+		mutex.Unlock()
+	End Method
+
+End Type
+
 Rem
 bbdoc: Create a thread
 returns: A new thread object.