Boost logo

Boost :

Subject: Re: [boost] Request for interest in the new Synchro library
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-02-24 15:55:43


----- Original Message -----
From: "Dmitry Goncharov" <dgoncharov_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, February 19, 2009 8:37 AM
Subject: Re: [boost] Request for interest in the new Synchro library

> vicente.botet wrote:
>>> What type of semaphore are you planning to have in your library?
>>>
>>
>> Hi,
>>
>> currently Synchro provides the same semaphore as Boost.Interprocess (counting semaphores), except that it uses Threads resources on a inter-threads context, and Interprocess resources on a inter-process context.
>>
>> The library could provide binary semaphores (the difference is minimal).Could you talk us about your use case, and why a mutex cannot be used in your context?
>>
>>
> I need to block a thread until either a timeout expires or some other
> thread wakes this one up. This is really what
> condition_variable::timed_wait() is for, right?
> Why can't condition_variable::timed_wait() be used? It can. It has a
> drawback, though.
> After the condvar was signaled it has to lock a mutex. This means there
> is an unbound timeout between the time condvar was signaled and the time
> condition_variable::timed_wait() returns.
> A binary_semaphore::timed_wait() can be used for the same purpose. The
> advantage is it doesn't have to lock a mutex.

Hi,

the binary_semaphore you describe must poll with test_and_set. I have no such implementation, but as far as a portable test_and_set is available this can be implemented in the library.

Waiting for that what do you think of using a mutex as a binary_semaphore:

template <typename ScopeTag=multi_threaded_tag>
class basic_binary_semaphore
{
    typedef synchronization_family<ScopeTag> Sync;
    typedef typename Sync::mutex_type lockable_type;
    lockable_type mtx_;
public:
    BOOST_COPY_CONSTRUCTOR_DELETE(basic_binary_semaphore) /*< disable copy construction >*/
    BOOST_COPY_ASSIGNEMENT_DELETE(basic_binary_semaphore) /*< disable copy asignement >*/

    inline basic_binary_semaphore() { mtx_.unlock(); };
    
    inline void post() { mtx_.unlock(); }

    inline void wait() { mtx.lock(); }

    inline bool try_wait() { return mtx.try_lock(); }

    inline bool try_wait_until(const system_time &abs_time) {
        return mtx.try_lock_until();
    }
    
    template<typename TimeDuration>
    inline bool try_wait_for(const TimeDuration &rel_time) {
        return try_wait_until(get_system_time()+rel_time);
    }
    
    inline void wait_until(const system_time &abs_time) {
        if (!try_wait_until(abs_time)) throw timeout_exception();
    }
    
    template<typename TimeDuration>
    inline void wait_for(const TimeDuration &rel_time) {
        if (!try_wait_until(rel_time)) throw timeout_exception();
    }
};

Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk