Parameters in asynchronous lambdas

后端 未结 2 545
清歌不尽
清歌不尽 2021-01-12 19:41

I am trying to run several tasks at the same time and I came across an issue I can\'t seem to be able to understand nor solve.

I used to have a function like this :<

2条回答
  •  半阙折子戏
    2021-01-12 20:36

    All your lambdas capture the same variable which is your loop variable. However, all your lambdas are executed only after the loop has finished. At that point in time, the loop variable has the maximum value, hence all your lambdas use it.

    Stephen Cleary shows in his answer how to fix it.

    Eric Lippert wrote a detailled two-part series about this.

提交回复
热议问题