Passing lambdas to std::thread and calling class methods

前端 未结 2 851
遥遥无期
遥遥无期 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:55

    This is because the objects cursor and a are passed by value to the constructor of thread. The functor takes a reference to the local copies of the newly created thread and not on the objects you expected them to be .

    Hence, as answered by "alexk7", you should use std::ref or if you want to capture them pass by reference

提交回复
热议问题