on a caught variable, and pass it to a second thread where it'll execute.. through say a shared vector/queue of functors between threads..
Does the lambda store the variable ..
I want to know the mechanics of memory here ? Some help please..
If the lambda captures a variable by value and the copy semantics of the type of the variable allows safe usage by multiple copies in threads then this is safe. Think of a lambda like an anonymous class that the compiler creates with captured variables as data members. If the value is captured by reference, it may or may not be safe depending on the lifetime of the referenced variable and also on safe usage between threads.
Std::bind works same way? Approximately?
There is no reason why std::bind should be used now that lambdas are available. The semantics of std::bind are different from that of a lambda. You can read Scott Meyers chapter on std::bind vs lambdas in his book Modern Effective C++ to know why std::bind should no like ngsr be used
Обсуждают сегодня