Passing lambdas to std::thread and calling class methods

前端 未结 2 840
遥遥无期
遥遥无期 2020-12-10 11:02

I\'m having a bit of trouble using std::thread together with lambdas. I have a method TheMethod where I should use std::thread to parallelize some function calls to methods

2条回答
  •  無奈伤痛
    2020-12-10 11:58

    You can use std::ref to pass the parameters by reference:

    std::thread t1(functor, std::ref(cursor), std::ref(a))
    

    You could also capture the parameters by reference in the lambda itself:

    size_t a;
    Cursor cursor = someCursor();
    std::thread t1([&] {a = classMethod(cursor);});
    t1.join();
    

提交回复
热议问题