跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • c++ thread 并发问题
未分類
21 7 月 2020

c++ thread 并发问题

c++ thread 并发问题

資深大佬 : Huelse 4

我想用 thread 并发处理 for 循环,但总是 join 放在循环外就会出错,是 lambda 的问题吗?

vector<thread> threads;  for (int k = 1; k < d; k++) {     thread t1([&](){         A_result[k] = Linear_Transform_Plain(A_result[0], params);     });     thread t2([&](){         B_result[k] = Linear_Transform_Plain(B_result[0], params);     });     threads.push_back(t1);     threads.push_back(t2);     // join 在里面就不会出错     // t1.join();     // t2.join(); } for(auto i = threads.begin(); i != threads.end(); i++){     i->join(); } 

报错:

terminate called after throwing an instance of 'std::bad_alloc'   what():  std::bad_alloc Aborted (core dumped) 

尝试过换用thread threads[14]然后索引赋值的方法,报错:

terminate called recursively Aborted (core dumped) 

然后不用 lambda 就可以运行,但获取不到返回值了

thread threads[14];  for (int k = 1; k < d; k++) {     threads[k-1] = thread(Linear_Transform_Plain, A_result[0], params);     threads[k-1+7] = thread(Linear_Transform_Plain, B_result[0], params); } for (int k = 1; k < d; k++) {     threads[k-1].join();     threads[k-1+7].join(); } 

网上找了大多是基础用法和线程池,并不能达到我想要的全核心并发处理。

这里先感谢各位了!

大佬有話說 (17)

  • 資深大佬 : hankai17

    thread 不支持 copy?

  • 主 資深大佬 : Huelse

    @hankai17 #1 是什么意思?

  • 資深大佬 : gantleman

    从 C++的语法来说你在 for 循环里声明了两个 thread t1 t2 的局部变量。
    离开 for 循环后这两个局部变量被销毁,任何使用的操作都是非法的。
    建议你改用 new 尝试下。

  • 資深大佬 : nightwitch

    第一个里面,不要用引用去捕获 k 的值,k 的值一直在变,而且当 for 循环结束以后,k 的生命周期结束,你的 lambda 里面的 k 就是空悬引用。

  • 資深大佬 : nightwitch

    @gantleman t1,t2 被 push 到 vector 里面去了啊

  • 資深大佬 : nannanziyu

    std::thread 没有拷贝构造函数

    http://www.cplusplus.com/reference/thread/thread/thread/
    3) copy constructor
    Deleted constructor form (thread objects cannot be copied).

    所以修改 threads.push_back(t1); 为 threads.push_back(std::move(t1))

  • 資深大佬 : V2WT

    “`
    #include <iostream>
    #include <thread>
    #include <vector>
    #include <algorithm>
    int main()
    {
    // vector container stores threads
    std::vector<std::thread> workers;
    for (int i = 0; i < 5; i++) {
    workers.push_back(std::thread([]()
    {
    std::cout << “thread functionn”;
    }));
    }
    std::cout << “main threadn”;

    // Looping every thread via for_each
    // The 3rd argument assigns a task
    // It tells the compiler we’re using lambda ([])
    // The lambda function takes its argument as a reference to a thread, t
    // Then, joins one by one, and this works like barrier
    std::for_each(workers.begin(), workers.end(), [](std::thread &t)
    {
    t.join();
    });

    return 0;
    }

    “`
    I found these code, from here:
    [click]( https://www.bogotobogo.com/cplusplus/C11/3_C11_Threading_Lambda_Functions.php).
    Hope this would help!

  • 資深大佬 : nannanziyu

    然后还有 4 说的,你的 k 不能传引用,要传值
    改成
    std::thread t1([&,k]() {
    });

  • 主 資深大佬 : Huelse

    @nannanziyu #6
    @nightwitch #4
    感谢感谢~
    综合两位的答案,将第一个例子改成
    “`
    std::thread t1([&, k](){
    A_result[k] = …
    });

    threads.push_back(std::move(t1));
    “`
    就可以了,核心跑满,舒服了~

  • 資深大佬 : gantleman

    @nightwitch 是的,6 是正确答案

  • 資深大佬 : GeruzoniAnsasu

    c++11 以后不要记得容器有 push_back 这个函数

    一律用 emplace_back,这个函数会自动转发左右值引用

    emplace_back 还有一个重载是用入参构造元素,所以可以

    threads.emplace_back([&](){WHATEVER})

  • 資深大佬 : billyzs

    @GeruzoniAnsasu 这个例子直接上 emplace_back()没问题,不过明确需要拷贝左值的时候 push_back()可读性更高
    [https://abseil.io/tips/112]( https://abseil.io/tips/112)

  • 資深大佬 : tusj

    threads.push_back(std::move(t1));

  • 資深大佬 : Sentan

    @nannanziyu 大佬,问下,用 std:move 处理 tread 那个对象,把他变成左值是为了什么,这样是会把这个对象的生命周期变长吗,还是说让他从栈空间变成堆空间了?

  • 資深大佬 : Wirbelwind

    @Sentan thread 只有移动语义,所以不能 copy,只能转让所有权,用 std::move 告诉编译器调用移动构造

  • 資深大佬 : Wirbelwind

    @Sentan https://yoshino.now.sh/posts/How-to-understanding-cpp11-rvalue-reference 可以看一下

  • 資深大佬 : Sentan

    @Wirbelwind 哦哦,明白了

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具