Anonymous user
Mutex: Difference between revisions
no edit summary
MaiconSoft (talk | contribs) (Added Delphi example) |
No edit summary |
||
Line 1,143:
}
</lang>
=={{header|Shale}}==
Shale includes a library that provides POSIX threads, semaphores and mutexes. Below is a really simple example usings threads and one mutex. There's a more complete example that includes semaphores available with the [https://github.com/sharkshead/shale Shale source code].
<lang Shale>#!/usr/local/bin/shale
thread library // POSIX threads, mutexes and semaphores
time library // We use its sleep function here.
// The threead code which will lock the mutex, print a message,
// then unlock the mutex.
threadCode dup var {
arg dup var swap =
stop lock thread::()
arg "Thread %d has the mutex\n" printf
stop unlock thread::()
} =
stop mutex thread::() // Create the mutex.
stop lock thread::() // Lock it until we've started the threads.
// Now create a few threads that will also try to lock the mutex.
1 threadCode create thread::()
2 threadCode create thread::()
3 threadCode create thread::()
4 threadCode create thread::()
// The threads are all waiting to acquire the mutex.
"Main thread unlocking the mutex now..." println
stop unlock thread::()
// Wait a bit to let the threads do their stuff.
1000 sleep time::() // milliseconds</lang>
{{out}}
<pre>Main thread unlocking the mutex now...
Thread 4 has the mutex
Thread 2 has the mutex
Thread 1 has the mutex
Thread 3 has the mutex</pre>
=={{header|Tcl}}==
|