Everething worked with online compilers, but not with Visual2019
I am creating small library and got problem with std::function. I use it because i need to pass function pointer or lamda to one of class setters, and use it in class method.
The problem is that std::function variable is NULL for all instances of class exept first, but setters were called for all. My class method calls that functions inside while(true) loop, that is in other thread, setters were called from main function and main thread.
Here are some parts of code:
place where i call function:
for (auto j = equalrange.first; j != equalrange.second; j++)
{
j->second->draw_updater();
if (j->second->on_update_fx != NULL)
j->second->on_update_fx();
else
std::cout << "NULL\n";
setter:
void OnUpdate(const std::function<void()> fx)
{on_update_fx = fx;};
What can cause that problem? All objects from equalrange range are not NULL
Small example:
#include <functional> #include <future> class MyClass { public: void SetFx(std::function <void()> func) { fx = func; } void do_work() { ft = std::async([this] { while (true) { if (fx != NULL) fx(); else { std::cout << "NULL\n"; } } }); } private: std::future <void> ft; std::function <void()> fx; }; int main() { int c = 10; MyClass a; MyClass b; a.SetFx([&]() {std::cout << "a " << std::endl; }); b.SetFx([&]() {std::cout << "b " << std::endl; }); a.do_work(); b.do_work(); }