|
Boost : |
From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2007-05-07 00:14:44
Gregory Dai wrote:
> On 5/6/07, Yuval Ronen <ronen_yuval_at_[hidden]> wrote:
>
> <snip>
>
> namespace boost {
>> class semaphore
>> {
>> public:
>> semaphore(std::size_t count) : m_count(count), m_mutex(),
>> m_condition() { }
>>
>> void lock()
>> {
>> mutex::scoped_lock sl(m_mutex);
>> while (m_count == 0)
>> m_condition.wait(sl);
>> assert(m_count > 0);
>> m_count--;
>> }
>>
>> void unlock()
>> {
>> mutex::scoped_lock sl(m_mutex);
>> m_count++;
>> m_condition.notify_one();
>> }
>>
>> private:
>> std::size_t m_count;
>> mutex m_mutex;
>> condition m_condition;
>> };
>>
>> } // namespace boost
>
>
> <snip>
>
> Could we use more conventional method names such as wait() and post()
> instead of lock() and unlock(), please? Even acquire() and release() sound
> better. In addition, I'd suggest two more member methods:
> try_wait/try_acquire(size_t timeout) and count().
lock() and unlock() are good for usage with scoped_lock (in the code I
posted I actually didn't use the semaphore with a scoped_lock, but I
think it's a common use case). As far as the additions you suggest - I
probably didn't wrote a full fledged semaphore, just what I needed to
implement the stuff I needed. I agree that if it makes it to be publicly
available, these edges should be smoothed.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk