Friday 3 April 2015

std::async, std::future and lambda functions to keep GUIs responsive

It has been quite some time since my last post :)

This week I submitted my first paper as a PhD student in TU Delft and I had to shot some video to show my work. I had to show some analytic routine that require some time to be computed but I wanted to keep my GUI responsive, so that the video would results nice.

Imagine that you have a class that is similar to the following one:
You may want to call a method of the class that it is slow to compute. If you do that in the following way the the (Not Responding) problem in your GUI will occur.
The easiest way to solve this problem is to use some cool features of C++11, std::future, std::async and the lambda functions. I use std::async to launch a thread where a lambda function is used to isolate the slow computation. A std::future is used to check if the asynchronous thread has completed the computation and meanwhile the application is refreshed.
there you go :)

2 comments :