| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- SuperStrict
- Framework brl.standardio
- Import brl.threads
- ' -----------------------------------------------------------------------------
- ' MAKE SURE "Threaded Build" IS CHECKED IN THE Program -> Build Options menu!
- ' -----------------------------------------------------------------------------
- ' -----------------------------------------------------------------------------
- ' Simple thread demo...
- ' -----------------------------------------------------------------------------
- ' Mutexes are used to allow only one thread to access a variable or object in
- ' memory.
- ' A thread (including the main program's thread) can call LockMutex
- ' on a mutex, and if another thread has already locked the mutex, it will
- ' wait until the other thread has unlocked the mutex, then gain the lock. It's
- ' important that a thread calls UnlockMutex when it's done what it needs to do!
- ' The main program's thread and the spawned thread will both attempt to lock
- ' this mutex throughout, waiting on each other if they can't lock it...
- Global Mutex:TMutex = CreateMutex ()
- ' Try commenting out the above line, then scroll back through the output of
- ' the program, and you should see that the threads are fighting for access to
- ' the output console, creating intermeshing, gibberish text...
- Print ""
- Print "NOTE: Output of the two threads may not always alternate between 'Main' and 'Thread'..."
- Print ""
- ' Create thread using Test () function and pass Null parameter...
- Local thread:TThread = CreateThread (Test, Null)
- ' The new thread has now started. Do some stuff in the main program...
- For Local a:Int = 1 To 100
- ' Other thread may be using the Print command (which isn't thread-friendly),
- ' so LockMutex will wait until the thread has unlocked the mutex, and then
- ' lock it so main program can call Print. It will then unlock the mutex so
- ' the other thread can continue if it's ready (ie. waiting at its own
- ' LockThread call)...
-
- If Mutex Then LockMutex (Mutex)
- Print "Main: " + a
- If Mutex Then UnlockMutex (Mutex)
- ' Note: You'd normally just do this like the Rem'd out code below! The
- ' "If Mutex" check here is to allow you to comment out the line
- ' "Global Mutex = CreateMutex ()" to see why mutexes are needed...
- ' You would also not normally use LockMutex so heavily, as it will
- ' slow things down if over-used...
-
- Rem
-
- LockMutex (Mutex)
- Print "Main: " + a
- UnlockMutex (Mutex)
-
- End Rem
-
- Next
- ' Other thread may still be running, so wait for it to end...
- WaitThread (thread)
- End
- ' -----------------------------------------------------------------------------
- ' Test function. Locks same mutex as main program, or waits until it
- ' can do so, calls Print, then unlocks the mutex so main program can
- ' lock it and proceed...
- ' -----------------------------------------------------------------------------
- Function Test:Object (data:Object)
- For Local a:Int = 1 To 100
- If Mutex Then LockMutex (Mutex)
- Print "--------> Thread: " + a
- If Mutex Then UnlockMutex (Mutex)
- Next
- End Function
|