how to handle async operations
May 29, 2013 at 4:47pm UTC
Hi,
I've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)
Thread should be the only way to do this:
1 2 3 4
btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};
the local variable
thrd is destructed right after it starts, which may cause a crash, so use
new operator:
1 2 3 4
btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};
The
thrd never get deleted
How would you solve problem like this?
Thanks.
May 29, 2013 at 5:37pm UTC
Last edited on May 29, 2013 at 5:49pm UTC
May 29, 2013 at 11:54pm UTC
That't it, Thanks.,
Topic archived. No new replies allowed.