user1244932
user1244932

Reputation: 8092

c++ analog for rust like synhronization

As I see on some video about rust, it have something like this (I use c++ instead of rust to show idea):

template<typename T>
class Synchronized {
    public:
        Synchronized(T);
        Something<T> get();
    private:
        std::mutex lock_;
};

and usage like this:

Synchronized<std::string> obj;

auto s = obj.get();
//after that you can work with s as with std::string
//and obj.lock_ in locked state, after s was destroyed
//obj.lock_ will be unlocked

Is boost or some other C++ popular library have such pattern implementation?

Upvotes: 3

Views: 612

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145259

The as-of-this-writing experimental boost::synchronized_value appears to be what you're looking for.

Upvotes: 7

Related Questions