threads.bmx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. SuperStrict
  2. Framework brl.standardio
  3. Import brl.threads
  4. ' -----------------------------------------------------------------------------
  5. ' MAKE SURE "Threaded Build" IS CHECKED IN THE Program -> Build Options menu!
  6. ' -----------------------------------------------------------------------------
  7. ' -----------------------------------------------------------------------------
  8. ' Simple thread demo...
  9. ' -----------------------------------------------------------------------------
  10. ' Mutexes are used to allow only one thread to access a variable or object in
  11. ' memory.
  12. ' A thread (including the main program's thread) can call LockMutex
  13. ' on a mutex, and if another thread has already locked the mutex, it will
  14. ' wait until the other thread has unlocked the mutex, then gain the lock. It's
  15. ' important that a thread calls UnlockMutex when it's done what it needs to do!
  16. ' The main program's thread and the spawned thread will both attempt to lock
  17. ' this mutex throughout, waiting on each other if they can't lock it...
  18. Global Mutex:TMutex = CreateMutex ()
  19. ' Try commenting out the above line, then scroll back through the output of
  20. ' the program, and you should see that the threads are fighting for access to
  21. ' the output console, creating intermeshing, gibberish text...
  22. Print ""
  23. Print "NOTE: Output of the two threads may not always alternate between 'Main' and 'Thread'..."
  24. Print ""
  25. ' Create thread using Test () function and pass Null parameter...
  26. Local thread:TThread = CreateThread (Test, Null)
  27. ' The new thread has now started. Do some stuff in the main program...
  28. For Local a:Int = 1 To 100
  29. ' Other thread may be using the Print command (which isn't thread-friendly),
  30. ' so LockMutex will wait until the thread has unlocked the mutex, and then
  31. ' lock it so main program can call Print. It will then unlock the mutex so
  32. ' the other thread can continue if it's ready (ie. waiting at its own
  33. ' LockThread call)...
  34. If Mutex Then LockMutex (Mutex)
  35. Print "Main: " + a
  36. If Mutex Then UnlockMutex (Mutex)
  37. ' Note: You'd normally just do this like the Rem'd out code below! The
  38. ' "If Mutex" check here is to allow you to comment out the line
  39. ' "Global Mutex = CreateMutex ()" to see why mutexes are needed...
  40. ' You would also not normally use LockMutex so heavily, as it will
  41. ' slow things down if over-used...
  42. Rem
  43. LockMutex (Mutex)
  44. Print "Main: " + a
  45. UnlockMutex (Mutex)
  46. End Rem
  47. Next
  48. ' Other thread may still be running, so wait for it to end...
  49. WaitThread (thread)
  50. End
  51. ' -----------------------------------------------------------------------------
  52. ' Test function. Locks same mutex as main program, or waits until it
  53. ' can do so, calls Print, then unlocks the mutex so main program can
  54. ' lock it and proceed...
  55. ' -----------------------------------------------------------------------------
  56. Function Test:Object (data:Object)
  57. For Local a:Int = 1 To 100
  58. If Mutex Then LockMutex (Mutex)
  59. Print "--------> Thread: " + a
  60. If Mutex Then UnlockMutex (Mutex)
  61. Next
  62. End Function