J. J. Lee

J. J. Lee jjlee3 jjlee3

Niner since 2009

Comments

  • C++ and Beyond 2012: Herb Sutter - C++ Concurrency

    This session is really cool, many thanks.
    Anyway I am a little bit confused about monitor<T>.

    I could understand why std::mutex is mutable (I mean thread safe under C++11).
    But why T is mutable?

    The all public interfaces of std::mutex would provide thread safe but what about T?
    I guess only const member functions would provide thread safe.

    Should mutable be a qualifier for that instance of std:mutex or the std:mutex class itself (C++1y)?
    Could mutable be a qualifier for a member function such as const (C++1y)?

    Which one is better?
    1. template <typename F> auto operator()(F f) const
    2. template <typename F> auto operator()(F f) mutable

    If const member T functions would provide thread safe, why bother to operate under std::lock_guard<mutex>?
    I know monitor<T> would be easier to provide a general solution under a thread safe way.

    But I might use a dangerous way like below for some critical cases (it might be very few)
    template <class T> class monitor : public T {
    private:
        mutable std::mutex m;
    public:
        monitor(T t_ = T{}) : T(t_) {}

        template <typename F> auto operator()(F f) ...
    };

    I might call some const member T functions directly for precondition checking whether I need to go for operator() under critical cases.