《C++ Concurrency in Action》读书笔记二 线程之间共享资源
发布日期:2021-05-07 23:34:52 浏览次数:22 分类:精选文章

本文共 5690 字,大约阅读时间需要 18 分钟。

???????????????

?????????????????????????????????????????Race Condition??????????????????????????????????????????????


1. ???????

???????????????????????????????????????????????????????????????????????

  • ???????Benign Race Condition?

    ???????????????????????????????????????????????????????????????????????????????

  • ?????????Problematic Race Condition?

    ??????????????????????????????????????????????????????????????????????????????A?????????B???????????????????????


  • 2. ?????????????

    ????????????????????????

    a. ??????Mutex?

    ??????????????????????????????????????????

    • ????????????????????????Lock??
    • ????????????????
    • ???????????????????????????????

    ?????C++?

    #include 
    #include
    #include
    class MutexSample {
    public:
    void add_to_list(int new_value);
    bool list_contains(int value_to_find);
    private:
    std::list
    some_list;
    std::mutex some_mutex;
    };
    void MutexSample::add_to_list(int new_value) {
    std::lock_guard
    guard(some_mutex);
    some_list.push_back(new_value);
    std::cout << "Added value: " << new_value << std::endl;
    }
    bool MutexSample::list_contains(int value_to_find) {
    std::lock_guard
    guard(some_mutex); return std::find(some_list.begin(), some_list.end(), value_to_find) != some_list.end(); } int main() { MutexSample mtx; std::vector
    threads; for (int i = 0; i < 5; ++i) { threads.push_back(std::thread([=, &mtx] { mtx.add_to_list(i); })); } std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join)); if (mtx.list_contains(3)) { std::cout << "Find the value" << std::endl; } }

    ??????????????????????????????????????????????????????????????????????????????


    3. ?????????????

    ?????????????????????????

    a. ???????????????????

    ????????????????????????????????????

    b. ??????????

    ?????????????????std::stack ? pop ???????????????????????????????????????

    • ? pop ??????
    • ?? no-throw ???????? move ?????
    • ? pop ????????
    • ?? a ??? b ? c?

    ?????C++?

    #include 
    #include
    #include
    #include
    template
    class ThreadSafeStack {
    private:
    std::stack
    data; mutable std::mutex m; public: ThreadSafeStack() {} ThreadSafeStack(const ThreadSafeStack& other) { std::lock_guard
    lock(other.m); data = other.data; } ThreadSafeStack& operator=(const ThreadSafeStack& other) = delete; void push(T new_value) { std::lock_guard
    lock(m); data.push(new_value); } std::shared_ptr
    pop() { std::lock_guard
    lock(m); if (data.empty()) { throw empty_stack(); } std::shared_ptr
    res(std::make_shared
    (data.top())); data.pop(); return res; } void pop(T& value) { std::lock_guard
    lock(m); if (data.empty()) { throw empty_stack(); } value = data.top(); data.pop(); } bool empty() const { std::lock_guard
    lock(m); return data.empty(); } };

    4. ??????????

    ?????

    ?????????????????????????????????????????????????????????

    ???????

  • ???????

    ?????????????????????????????????????? std::lock() ???????????

  • ?????????????????

    ???????????????????????????????????????????????????

  • ???????????

    ????????????????????????????????? std::lock() ???????????

  • ?????

    ????????????????????????????? std::recursive_mutex ?????????

  • ?????C++?

    #include 
    #include
    class DeadLock {
    private:
    std::mutex m;
    public:
    void do_something() {
    std::lock_guard
    lock(m);
    std::cout << "do something!" << std::endl;
    do_something_else();
    }
    void do_something_else() {
    std::lock_guard
    lock(m);
    std::cout << "do something else" << std::endl;
    }
    };

    5. ?????????

    a. ?????? std::lock ? std::lock_guard

    std::lock ? std::lock_guard ????????????????????????

    b. ????????????

    ???? std::unique_lock ?????? std::move ??????????

    std::unique_lock
    get_lock() {
    extern std::mutex some_mutex;
    std::unique_lock
    lk(some_mutex);
    prepare_data();
    return lk;
    }
    void process_data() {
    std::unique_lock
    lk(get_lock());
    do_something();
    }

    c. ??????????

    ???????????????????????????

    std::unique_ptr
    lk(some_mutex);
    lk.lock();
    // ?????...
    lk.unlock();
    lk.lock();
    // ???????...
    lk.unlock();

    6. ?? std::unique_lock ??????

    std::unique_lock ? std::lock_guard ??????????????????????

    ?????C++?

    std::unique_lock
    lock_a(m1, std::deter_lock);
    std::unique_lock
    lock_b(m2, std::deter_lock);
    std::lock(lock_a, lock_b);

    7. ???????????

    ???????????????????????????

  • ?? std::shared_ptr ? std::unique_ptr ????????
  • ?? std::once_flag ? std::call_once ???????????????
  • ?????C++?

    #include 
    #include
    #include
    std::shared_ptr
    resource_ptr;
    std::once_flag resource_flag;
    void init_resource() {
    resource_ptr.reset(new int(42));
    }
    void foo() {
    std::call_once(resource_flag, init_resource);
    resource_ptr->operator*()();
    }
    int main() {
    foo();
    foo();
    return 0;
    }

    8. ????????????

    class X {
    private:
    connection_info connection_details;
    connection_handle connection;
    std::once_flag connection_init_flag;
    void open_connection() {
    connection = connection_manager.open(connection_details);
    }
    public:
    X(connection_info const& connection_details_) :
    connection_details(connection_details_)
    {}
    void send_data(data_packet const& data) {
    std::call_once(connection_init_flag, &X::open_connection, this);
    connection.send_data(data);
    }
    data_packet receive_data() {
    std::call_once(connection_init_flag, &X::open_connection, this);
    return connection.receive_data();
    }
    };

    ??????????????????????????????????????????????????

    上一篇:《C++ Concurrency in Action》读书笔记三 同步并发操作
    下一篇:《C++ Concurrency in Action》读书笔记一 多线程与线程管理

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年05月08日 15时23分27秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章

    2025版最新小白学习大模型:什么是大模型?零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新常用黑客工具之【Nmap 教程基础】零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新开发一款大模型需要经过哪些步骤?开发一款大模型的完整流程,收藏这篇就够了 2025-03-30
    $.inArray函数判断数组中的是否包含字符串 2025-03-30
    2025版最新渗透测试和黑客工具列表,零基础入门到精通,收藏这一篇就够了 2025-03-30
    2025版最新网络安全入门书籍整理大全,零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新网络安全知识入门及学习流程(非常详细)零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新网络安全等级保护测评指南,零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新运维怎么转行网络安全?零基础入门到精通,收藏这篇就够了 2025-03-30
    2025版最新黑客学习网站(非常详细),零基础入门到精通,看这一篇就够了 2025-03-30
    2025版网络工程11个高含金量证书(非常详细)零基础入门到精通,收藏这篇就够了 2025-03-30
    2025自学成为黑客必读的5本书籍,带你从小白进阶成大佬 2025-03-30
    20万高薪专业-网络安全(非常详细)零基础入门到精通,收藏这一篇就够了 2025-03-30
    23张图告诉你组建一个网络需要用到哪些硬件设备?路由器、交换机、防火墙是不是就够了? 2025-03-30
    24 WEB漏洞-文件上传之WAF绕过及安全修复_阿里云盾waf绕过怎么修复 2025-03-30
    #12 btrfs文件系统 2025-03-30
    #3194. 去月球 2025-03-30
    24.线程 2025-03-30
    #Leetcode# 28. Implement strStr() 2025-03-30
    $route 和 $router详解、区别、示例代码 2025-03-30