31 lines
882 B
Plaintext
31 lines
882 B
Plaintext
#!/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
|