future_01.bmx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. SuperStrict
  2. Framework brl.standardio
  3. Import brl.threads
  4. ' Define a function to perform some computation in a background thread
  5. Function ComputeSomethingAsync:Object( data:Object )
  6. Local future:TFuture<Float> = TFuture<Float>(data)
  7. ' Simulate a time-consuming computation with Delay
  8. Local result:Float = 0.0
  9. For Local i:Int = 0 Until 10
  10. result :+ 0.1
  11. Print "Computing... " + result
  12. Delay(500)
  13. Next
  14. ' Set the result in the future object
  15. future.SetResult( result )
  16. End Function
  17. ' Create a TFuture instance for Float
  18. Local future:TFuture<Float> = New TFuture<Float>
  19. ' Start the computation in a background thread
  20. Local thread:TThread = CreateThread( ComputeSomethingAsync, future )
  21. ' Simulate doing some other work in the main thread
  22. Print "Main thread is doing other work..."
  23. For Local j:Int = 0 Until 5
  24. Print "Main work step " + j
  25. Delay(300)
  26. Next
  27. ' Wait for and retrieve the result from the future object
  28. Local result:Float = future.GetResult()
  29. Print "The result of the computation is: " + result
  30. ' Wait for the background thread to finish
  31. thread.Wait()