加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 带有指向数据成员的指针的std :: thread

发布时间:2020-12-16 05:03:29 所属栏目:百科 来源:网络整理
导读:我正在阅读 std::thread documentation at cppreference(并不总是100%准确,我知道)并注意到以下定义std :: thread在传递“指向数据成员”时的行为(不是“指向成员的指针”) function“)作为它的第一个参数(f)和一个必需类的对象作为它的第二个参数(复制到th
我正在阅读 std::thread documentation at cppreference(并不总是100%准确,我知道)并注意到以下定义std :: thread在传递“指向数据成员”时的行为(不是“指向成员的指针”) function“)作为它的第一个参数(f)和一个必需类的对象作为它的第二个参数(复制到thread-local-storage后的t1):

If N == 1 and f is pointer to a member data object of a class,then it is accessed. The value of the object is ignored. Effectively,the following code is executed:
t1.*f if and the type of t1 is either T,reference to T or reference to type derived from T.
(*t1).*f otherwise.

现在,我不打算以这种方式使用std :: thread,但是我对这个定义感到沮丧.显然,唯一发生的事情是访问数据成员并忽略值,这似乎根本就没有任何可观察到的副作用,这意味着(据我所知)它可能也是一个无操作. (我可能会遗漏一些明显的东西……?)

起初,我认为这可能是一个错误打印,并且意味着数据成员被访问然后被调用(因为它可能是一个可调用的对象,即使它不是一个函数)但我在GCC中使用以下代码对其进行了测试-4.7确实没有电话:

#include <iostream>
#include <thread>

struct S 
{
    void f() {
        std::cout << "Calling f()" << std::endl;
    }

    struct {
        void operator()() {
            std::cout << "Calling g()" << std::endl;
        }
    } g;
};

int main(int,char**)
{
    S s;
    s.f(); // prints "Calling f()"
    s.g(); // prints "Calling g()"

    std::cout << "----" << std::endl;

    auto x = &S::f; // ptr-to-mem-func
    auto y = &S::g; // ptr-to-data-mem

    (s.*x)(); // prints "Calling f()"
    (s.*y)(); // prints "Calling g()"

    std::cout << "----" << std::endl;

    std::thread t(x,&s);
    t.join();
    // "Calling f()" printed by now
    std::thread u(y,&s);
    u.join();
    // "Calling g()" not printed

    return 0;
}

这个定义有什么目的似乎没有任何成就吗?为什么不将“指向数据成员可调用指针”的行为传递给“指向成员函数的指针”,并将“指向数据成员 – 不可调用的指针”传递给错误?实际上,似乎这是实现它的最简单方法,因为调用“指向数据成员可调用指针”具有与在其他上下文中调用“指向成员函数的指针”相同的语法(除非在模板专业化和SFINAE规则的混乱中存在某些因素,这使得难以等同地对待它们……?)

这不是我需要的实际代码,但这个定义存在的事实让我怀疑我遗漏了一些基本的东西,这让我担心……有人可以启发我吗?

解决方法

这是因为通用绑定工具,C 11标准不仅定义了线程的启动方式,还定义了std :: bind和std :: function的工作方式.

事实上,C 11标准的第30.3.1.2/3段规定了类std :: thread的可变构造函数:

template <class F,class ...Args> explicit thread(F&& f,Args&&... args);

Effects: Constructs an object of type thread. The new thread of execution executes INVOKE (DECAY_-
COPY ( std::forward<F>(f)),DECAY_COPY (std::forward<Args>(args))...)
with the calls to
DECAY_COPY being evaluated in the constructing thread. Any return value from this invocation is
ignored. […]

忽略DECAY_COPY的作用(与问题无关),这是第20.8.2段定义INVOKE伪函数的方式:

Define INVOKE (f,t1,t2,…,tN) as follows:

— (t1.*f)(t2,tN) when f is a pointer to a member function of a class T and t1 is an object of
type T or a reference to an object of type T or a reference to an object of a type derived from T;

— ((*t1).*f)(t2,tN) when f is a pointer to a member function of a class T and t1 is not one of
the types described in the previous item;

— t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a
reference to an object of type T or a reference to an object of a type derived from T;

(*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types
described in the previous item;

— f(t1,tN) in all other cases.

现在问题变成:

Why does the C++11 Standard defines the INVOKE facility that way?

答案是here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读