C++ Thread function pass by reference

后端 未结 1 1490
名媛妹妹
名媛妹妹 2020-12-22 12:41

I wonder how we can add a function passing by reference to thread.. Integer examples are so easy at Internet i\'ve found, but couldn\'t find any example pass by reference ?<

相关标签:
1条回答
  • 2020-12-22 13:10

    Solution 1: use std::ref:

        int number1 = 25;
        int number2 = 50;
        std::thread third(addp, std::ref(number1), std::ref(number2));
    

    Solution 2: use a lambda:

        int number1 = 25;
        int number2 = 50;
        std::thread third([&] { addp(number1, number2); });
    
    0 讨论(0)
提交回复
热议问题