threads.bmx 2.9 KB

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