码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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节的时候也有讲过,不知道为什么这里又犯了这个错。

  • 相关阅读:
    Day45-9大内置对象、JSP标签、JSTL标签、EL表达式、JavaBean
    【设计模式深度剖析】【7】【结构型】【享元模式】| 以高脚杯重复使用、GUI中的按钮为例说明,并对比Java类库设计加深理解
    【kafka】记一次kafka基于linux的原生命令的使用
    express学习33-多人管理25node.js—安装bcrypt出现错误的解决办法
    MySQL高级
    NLP实践——Few-shot事件抽取《Building an Event Extractor with Only a Few Examples》
    HTML5期末考核大作业,网站——青岛民俗 7页。 美丽家乡 学生旅行 游玩 主题住宿网页
    电影票房排名查询易语言代码
    U2Net网络简介
    c#仿热血江湖
  • 原文地址:https://blog.csdn.net/wang1902568721/article/details/126929215
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号