vendredi 26 février 2016

C++11 threading: platform agnostic way to run std::asynch on main thread

This question has been asked before, but the accepted answer is all but platform neutral, nor does it fit into usual applications (where you have no control over the main thread loop).

So what I'm after is rather similar to what libdispatch does (specifying the main thread queue to run something there triggered from a background thread), but with pure C++11. I would also like to avoid timers as they usually have a very low priority and may execute unreliably (depending on the load of the main thread).

The usage could be something as simple as:

run_on_main_thread([]() { myGui.refresh(); };

called in a background thread after finishing some work. Ideally, it would allow both: synchronous and asynchronous execution.

Of course I'm not asking for a full solution, but rather some ideas how this can be implemented with pure C++11. I can even imagine smaller platform dependent parts (in a neutral wrapper) which allow to control the execution (like the native handle from a thread). But I don't see how to make a task run on a specific thread (be it the main thread or a manually created background thread).

Aucun commentaire:

Enregistrer un commentaire