class semaphore public: semaphore(bool init) void get() void put() private: bool available
void semaphore::get() < await available ; available = false >
void semaphore::put() available = true
semaphore(bool a) available = a
get()
atomicity can be implemented with a critical section, or (more
likely) by the host environment (language or operating system).
put()
may have to be atomic too.
put()
s and get()
s.
#s.get()
is the number of completed get()
calls made on
semaphore s
.
#s.put()
.
0 <= #s.put() - #s.get() <= 1
Computations in cr = #s.put() - #s.get()
get()
s.
bool try_get() < if available then available = false return true else return false >
class counting_semaphore public: semaphore(unsigned init = 0) cnt = init; void get() < await cnt > 0 ; cnt-- > void put() < cnt = cnt + 1 > private: // 0 <= #put() - #get() <= init unsigned cnt;
init = 1
;
This page last modified on 1 July 2003.