• C++并发编程实战2.6程序问题解答


    C++并发编程实战2.6程序问题解答

    问题

    《C++并发编程实战》第二版中的代码2.6,作者实现了如下代码

    #include 
    #include 
    
    class scoped_thread
    {
        std::thread t;
    public:
        explicit scoped_thread(std::thread t_):
            t(std::move(t_))
        {
            if(!t.joinable())
                throw std::logic_error("No thread");
        }
        ~scoped_thread()
        {
            t.join();
        }
        scoped_thread(scoped_thread const&)=delete;
        scoped_thread& operator=(scoped_thread const&)=delete;
    };
    
    void do_something(int& i)
    {
        ++i;
    }
    
    struct func
    {
        int& i;
    
        func(int& i_):i(i_){}
    
        void operator()()
        {
            for(unsigned j=0;j<1000000;++j)
            {
                do_something(i);
            }
        }
    };
    
    void do_something_in_current_thread()
    {}
    
    void f()
    {
        int some_local_state;
        scoped_thread t(std::thread(func(some_local_state)));
            
        do_something_in_current_thread();
    }
    
    int main()
    {
        f();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    这个代码不管在VS中还是g++中都无法运行,虽然编译和链接没报错,但是打断点发现scoped_thread的构造函数和线程函数都没有运行。

    解决方案

    void f()
    {
        int some_local_state;
        scoped_thread t(std::thread((func(some_local_state))));
        do_something_in_current_thread();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在func前后再加一对括号即可

    问题原因

    测试了很多次,我个人理解,应该是跟C++'s most vexing parse有关。它的大概意思就是:
    C++ 标准规定把这样的 statement 视作函数声明。

    T1 name1(T2(name2));
    
    • 1

    根据 C++ 标准,此时不把 T2(name2) 视为「a function style cast」,而将其视为 T2 name2,这样整个语句就变成:T1 name1(T2 name2);,显然这是个函数声明。
    类似地,T1 name1(T2(name2), T3(name3));被视作 T1 name1(T2 name2, T3 name3)

    如果增加这么一个函数,

    std::thread func(int& i)
    {
        return std::thread(do_something_in_current_thread);
    }
    
    • 1
    • 2
    • 3
    • 4

    设置断点,会发现scoped_thread走的就是新加的这个函数

     scoped_thread t(std::thread(func(some_local_state)));
    
    • 1

    也就是说,上面这行代码,本意是想创建一个临时的std::thread对象,实际上,却被C++解释成了,调用std::thread func(some_local_state)函数,这个函数返回一个std::thread对象,接收一个int类型的参数。

    加括号表示是一个表达式,对其可以求值。而不加括号被编译器理解成一个函数,所以要加括号。
    Fctor() :可以理解成函数名字为Fctor的函数。
    (Fctor()): 作为表达式。
    这个问题实际上作者在2.1.1节的时候也有讲过,不知道为什么这里又犯了这个错。

  • 相关阅读:
    【loadrunner】form表单模式提交例子
    About Estimation with Cross-Validation
    CS224W6.2——深度学习基础
    codeforces 1239D-Catowice City(tarjan缩点)
    STM32单片机OLED经典2048游戏单片机小游戏
    关于ESP32烧录的问题 - No serial data received
    入行数字IC验证的一些建议
    大学经典题目:Java输出杨辉三角形
    【CentOS安装配置Jupyter 】
    微信小程序订阅消息
  • 原文地址:https://blog.csdn.net/wang1902568721/article/details/126929215