Does passing parameters in lambda's capture to boost asio post/dispatch thread safe?












0














I'm using lambda's capture in order to pass parameters to boost::asio::io_context::post callback.

Is it thread safe?



Code



#include <iostream>
#include "boost/asio.hpp"
#include <thread>

int main() {
boost::asio::io_service io_service;
boost::asio::io_service::work work(io_service);

std::thread t([&](){
io_service.run();
});

auto var = 1;
io_service.post([&io_service, var]() {
std::cout << "v: " << var << std::endl;
io_service.stop();
});

t.join();

return 0;
}


As you can see, I pass var in the lambda's capture.
the main thread sets var's value, and thread t reads it.

I didn't use any of memory ordering, for example, std::memory_order_release after setting var to 1, and std::memory_order_acquire before reading var value. Even more, I don't think I can - because the variable var is passed by value to the lambda.



Is it safe to do that?

If not, how should it be done?










share|improve this question



























    0














    I'm using lambda's capture in order to pass parameters to boost::asio::io_context::post callback.

    Is it thread safe?



    Code



    #include <iostream>
    #include "boost/asio.hpp"
    #include <thread>

    int main() {
    boost::asio::io_service io_service;
    boost::asio::io_service::work work(io_service);

    std::thread t([&](){
    io_service.run();
    });

    auto var = 1;
    io_service.post([&io_service, var]() {
    std::cout << "v: " << var << std::endl;
    io_service.stop();
    });

    t.join();

    return 0;
    }


    As you can see, I pass var in the lambda's capture.
    the main thread sets var's value, and thread t reads it.

    I didn't use any of memory ordering, for example, std::memory_order_release after setting var to 1, and std::memory_order_acquire before reading var value. Even more, I don't think I can - because the variable var is passed by value to the lambda.



    Is it safe to do that?

    If not, how should it be done?










    share|improve this question

























      0












      0








      0







      I'm using lambda's capture in order to pass parameters to boost::asio::io_context::post callback.

      Is it thread safe?



      Code



      #include <iostream>
      #include "boost/asio.hpp"
      #include <thread>

      int main() {
      boost::asio::io_service io_service;
      boost::asio::io_service::work work(io_service);

      std::thread t([&](){
      io_service.run();
      });

      auto var = 1;
      io_service.post([&io_service, var]() {
      std::cout << "v: " << var << std::endl;
      io_service.stop();
      });

      t.join();

      return 0;
      }


      As you can see, I pass var in the lambda's capture.
      the main thread sets var's value, and thread t reads it.

      I didn't use any of memory ordering, for example, std::memory_order_release after setting var to 1, and std::memory_order_acquire before reading var value. Even more, I don't think I can - because the variable var is passed by value to the lambda.



      Is it safe to do that?

      If not, how should it be done?










      share|improve this question













      I'm using lambda's capture in order to pass parameters to boost::asio::io_context::post callback.

      Is it thread safe?



      Code



      #include <iostream>
      #include "boost/asio.hpp"
      #include <thread>

      int main() {
      boost::asio::io_service io_service;
      boost::asio::io_service::work work(io_service);

      std::thread t([&](){
      io_service.run();
      });

      auto var = 1;
      io_service.post([&io_service, var]() {
      std::cout << "v: " << var << std::endl;
      io_service.stop();
      });

      t.join();

      return 0;
      }


      As you can see, I pass var in the lambda's capture.
      the main thread sets var's value, and thread t reads it.

      I didn't use any of memory ordering, for example, std::memory_order_release after setting var to 1, and std::memory_order_acquire before reading var value. Even more, I don't think I can - because the variable var is passed by value to the lambda.



      Is it safe to do that?

      If not, how should it be done?







      multithreading c++11 boost synchronization boost-asio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 19:33









      hudac

      96621240




      96621240
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It is thread-safe.



          Closure object is created by main thread (with copying var value) after var was created and initialized.
          Next, closure object is passed as argument to post method which queues this function object and returns immediately without calling functor. Functor is called between post and t.join calls - post guarantees it.
          So your code must be thread-safe.



          You would need some synchronization method (for example, use of mutex+lock_guard)
          if var was passed by reference [1] and some writing operations on var [2]
          were performed between post and t.join calls:



          auto var = 1;
          io_service.post([&io_service, &var]() { // [1] takes by reference
          std::cout << "v: " << var << std::endl; // lock mutex for printing
          io_service.stop();
          });
          var = 10; // [2] , lock mutex for writing
          // synchronization must be added because between post and t.join calls,
          // reading and writing operations are executed
          t.join();


          in this case you would have to protect var.






          share|improve this answer





















          • So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
            – hudac
            Nov 13 at 7:41










          • If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
            – rafix07
            Nov 13 at 7:56











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268925%2fdoes-passing-parameters-in-lambdas-capture-to-boost-asio-post-dispatch-thread-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          It is thread-safe.



          Closure object is created by main thread (with copying var value) after var was created and initialized.
          Next, closure object is passed as argument to post method which queues this function object and returns immediately without calling functor. Functor is called between post and t.join calls - post guarantees it.
          So your code must be thread-safe.



          You would need some synchronization method (for example, use of mutex+lock_guard)
          if var was passed by reference [1] and some writing operations on var [2]
          were performed between post and t.join calls:



          auto var = 1;
          io_service.post([&io_service, &var]() { // [1] takes by reference
          std::cout << "v: " << var << std::endl; // lock mutex for printing
          io_service.stop();
          });
          var = 10; // [2] , lock mutex for writing
          // synchronization must be added because between post and t.join calls,
          // reading and writing operations are executed
          t.join();


          in this case you would have to protect var.






          share|improve this answer





















          • So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
            – hudac
            Nov 13 at 7:41










          • If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
            – rafix07
            Nov 13 at 7:56
















          1














          It is thread-safe.



          Closure object is created by main thread (with copying var value) after var was created and initialized.
          Next, closure object is passed as argument to post method which queues this function object and returns immediately without calling functor. Functor is called between post and t.join calls - post guarantees it.
          So your code must be thread-safe.



          You would need some synchronization method (for example, use of mutex+lock_guard)
          if var was passed by reference [1] and some writing operations on var [2]
          were performed between post and t.join calls:



          auto var = 1;
          io_service.post([&io_service, &var]() { // [1] takes by reference
          std::cout << "v: " << var << std::endl; // lock mutex for printing
          io_service.stop();
          });
          var = 10; // [2] , lock mutex for writing
          // synchronization must be added because between post and t.join calls,
          // reading and writing operations are executed
          t.join();


          in this case you would have to protect var.






          share|improve this answer





















          • So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
            – hudac
            Nov 13 at 7:41










          • If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
            – rafix07
            Nov 13 at 7:56














          1












          1








          1






          It is thread-safe.



          Closure object is created by main thread (with copying var value) after var was created and initialized.
          Next, closure object is passed as argument to post method which queues this function object and returns immediately without calling functor. Functor is called between post and t.join calls - post guarantees it.
          So your code must be thread-safe.



          You would need some synchronization method (for example, use of mutex+lock_guard)
          if var was passed by reference [1] and some writing operations on var [2]
          were performed between post and t.join calls:



          auto var = 1;
          io_service.post([&io_service, &var]() { // [1] takes by reference
          std::cout << "v: " << var << std::endl; // lock mutex for printing
          io_service.stop();
          });
          var = 10; // [2] , lock mutex for writing
          // synchronization must be added because between post and t.join calls,
          // reading and writing operations are executed
          t.join();


          in this case you would have to protect var.






          share|improve this answer












          It is thread-safe.



          Closure object is created by main thread (with copying var value) after var was created and initialized.
          Next, closure object is passed as argument to post method which queues this function object and returns immediately without calling functor. Functor is called between post and t.join calls - post guarantees it.
          So your code must be thread-safe.



          You would need some synchronization method (for example, use of mutex+lock_guard)
          if var was passed by reference [1] and some writing operations on var [2]
          were performed between post and t.join calls:



          auto var = 1;
          io_service.post([&io_service, &var]() { // [1] takes by reference
          std::cout << "v: " << var << std::endl; // lock mutex for printing
          io_service.stop();
          });
          var = 10; // [2] , lock mutex for writing
          // synchronization must be added because between post and t.join calls,
          // reading and writing operations are executed
          t.join();


          in this case you would have to protect var.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 21:49









          rafix07

          6,6131613




          6,6131613












          • So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
            – hudac
            Nov 13 at 7:41










          • If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
            – rafix07
            Nov 13 at 7:56


















          • So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
            – hudac
            Nov 13 at 7:41










          • If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
            – rafix07
            Nov 13 at 7:56
















          So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
          – hudac
          Nov 13 at 7:41




          So, are you saying: as the lambda passes into thread t and get queued and executed successfully, the variable also passes?
          – hudac
          Nov 13 at 7:41












          If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
          – rafix07
          Nov 13 at 7:56




          If var is passed by value to lambda, closure object holds it as data member (copy of this var), so while passing closure to thread , we can say that the copy of var is passed.
          – rafix07
          Nov 13 at 7:56


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268925%2fdoes-passing-parameters-in-lambdas-capture-to-boost-asio-post-dispatch-thread-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python