mutex_wrapped是什么?

huangapple go评论74阅读模式
英文:

What is mutex_wrapped?

问题

In this tutorial, they discuss the usage of generic lambdas in C++. The first code snippet demonstrates visiting syntax:

boost::variant<int, double> value;
apply_visitor(value, [&](auto&& e){
   std::cout << e;
});

The second code snippet involves a mutex_wrapped class, and you're correct; it appears to be an imaginary class with a write method that takes a lambda and executes it using the wrapped stream as a parameter. This part of the example may not be representative of standard C++ usage.

For your first observation, it seems like there might have been changes to the Boost library that required swapping the operands in the boost::apply_visitor call:

boost::variant<int, double> value;
boost::apply_visitor(
    [&](auto&& e){
        std::cout << e;
    },
    value
);

However, as you mentioned, it's possible that the tutorial's content has become outdated since it was written.

英文:

In this tutorial they state:

> A strong reason to use generic lambdas is for visiting syntax.
>
> boost::variant<int, double> value;
> apply_visitor(value, [&](auto&& e){
> std::cout << e;
> });
>
> Here we are visiting in a polymorphic manner; but in other contexts,
> the names of the type we are passing isn't interesting:
>
> mutex_wrapped<std::ostream&> os = std::cout;
> os.write([&](auto&& os){
> os << "hello world\n";
> });
>
> Repeating the type of std::ostream& is noise here; it would be like
> having to mention the type of a variable every time you use it. Here
> we are creating a visitor, but no a polymorphic one; auto is used for
> the same reason you might use auto in a for(:) loop.

First of all I could not get the above example to work with out swapping the operands around:

boost::variant&lt;int, double&gt; value;
boost::apply_visitor(
    [&amp;](auto&amp;&amp; e){
        std::cout &lt;&lt; e;
    },
    value
);

I suppose this could be due to changes to boost since the tutorial was written.

But for the second example I cannot yet find from where mutex_wrapped comes, and I do not understand what write(lambda) will do, it seems to me that write() requires const char* s, streamsize n.

Is this second example purely imaginary as in mutex_wrapped is an imaginary class that has a write method that takes a lambda and executes the lambda using the wrapped stream as the parameter?

答案1

得分: 0

免责声明该网站:

本教程是从StackOverflow文档编译而来,内容由Stack Overflow的杰出人士撰写。

通过在StackOverflow上搜索,我找到了这个发布的答案:https://stackoverflow.com/a/59601498/6752050。它定义了shared_mutex_wrapped,但示例中将其用作mutex_wrapped,这可能是一个拼写错误。让我们将其命名为mutex_wrapped

template<class T>
struct mutex_wrapped {
  mutex_wrapped(T t) : t(t) {}  // 在原始帖子中缺少此部分

  template<class F>
  auto read( F&& f ) const {
    auto l = lock();
    return f(t);
  }

  template<class F>
  auto write( F&& f ) {
    auto l = lock();
    return f(t);
  }

private:
  mutable std::shared_mutex m;
  T t = {};
  auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
  auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};
英文:

Declaimer of that site:

> This Tutorial is compiled from StackOverflow Documentation, the content is written by the brilliant people at Stack Overflow.

Googling trough StackOverflow gave me that posted answer: https://stackoverflow.com/a/59601498/6752050. It defined shared_mutex_wrapped but the example used it as mutex_wrapped, it might be a typo. Let's name it mutex_wrapped:

template&lt;class T&gt;
struct mutex_wrapped {
  mutex_wrapped(T t) : t(t) {}  // Was missing in the original post

  template&lt;class F&gt;
  auto read( F&amp;&amp; f ) const {
    auto l = lock();
    return f(t);
  }

  template&lt;class F&gt;
  auto write( F&amp;&amp; f ) {
    auto l = lock();
    return f(t);
  }

private:
  mutable std::shared_mutex m;
  T t = {};
  auto lock() const { return std::shared_lock&lt; std::shared_mutex &gt;( m ); }
  auto lock() { return std::unique_lock&lt; std::shared_mutex &gt;( m ); }
};

答案2

得分: 0

The Wrapper needs a generic constructor to assign its internal type T, t variable. Here's a complete corrected example:

#include <iostream>
#include <mutex>
#include <shared_mutex>

template<class T>
struct mutex_wrapped {
    explicit mutex_wrapped(T t) : t(t) {}

    template<class F>
    auto read( F&& f ) const {
        auto l = lock();
        return f(t);
    }
    
    template<class F>
    auto write( F&& f ) {
        auto l = lock();
        return f(t);
    }

private:
    mutable std::shared_mutex m;
    T t = {};
    
    auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
    auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};

int main() {
    mutex_wrapped<std::ostream&&> os(std::cout);
    os.write([&](auto&& os){
        os << "hello world\n";
    });

    return 0;
}

mutex_wrapped是什么?
mutex_wrapped是什么?

英文:

The Wrapper needs a generic constructor to assign it's internal type T, t variable. Here's a complete corrected example:

#include &lt;iostream&gt;
#include &lt;mutex&gt;
#include &lt;shared_mutex&gt;
    
template&lt;class T&gt;
struct mutex_wrapped {
    explicit mutex_wrapped(T t) : t(t) {}

    template&lt;class F&gt;
    auto read( F&amp;&amp; f ) const {
        auto l = lock();
        return f(t);
    }
    template&lt;class F&gt;
    auto write( F&amp;&amp; f ) {
        auto l = lock();
        return f(t);
    }
private:
    mutable std::shared_mutex m;
    T t = {};
    auto lock() const { return std::shared_lock&lt; std::shared_mutex &gt;( m ); }
    auto lock() { return std::unique_lock&lt; std::shared_mutex &gt;( m ); }
};

int main() {
    mutex_wrapped&lt;std::ostream&amp;&gt; os(std::cout);
    os.write([&amp;](auto&amp;&amp; os){
        os &lt;&lt; &quot;hello world\n&quot;;
    });

    return 0;
}

mutex_wrapped是什么?
mutex_wrapped是什么?

huangapple
  • 本文由 发表于 2023年5月11日 11:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定