Mutual Exclusion

mutex()

mutex() creates a Mutex object.

mutex.lock()
mutex.unlock()

lock() locks the target mutex. unlock() unlocks the target mutex.

e.g.
buf = Object[1]
full = mutex()
empty = mutex()
counter = 0
empty.lock()

function producer(){
    for (i = 0; i < 10; ++i){
	full.lock()
	buf[0] = currentThread()
	++::counter
	println("produce:" + counter)
	flush()
	empty.unlock()
    }
}

function consumer(){
    for (i = 0; i < 10; ++i){
	empty.lock()
	buf[0] = null
	--::counter
	println("consume:" + counter)
	flush()
	full.unlock()
    }
}

fork(consumer)
fork(producer)

Back