Files
MapLibreParticleLab/include/mbgl/util/run_loop.hpp
Konstantin Käfer bd531b9e08 guarantee that Object stays around
Adds more documentation, and guarantees that the Object in the managed thread stays around until the Thread<> destructor is called. This might happen if .start() returns before .stop() is called, e.g. when a run loop in Object() terminates prematurely.
2015-04-10 15:52:02 +02:00

53 lines
1.0 KiB
C++

#ifndef MBGL_UTIL_RUN_LOOP
#define MBGL_UTIL_RUN_LOOP
#include <mutex>
#include <functional>
#include <queue>
namespace uv {
class async;
class loop;
}
namespace mbgl {
namespace util {
template <typename T> class Thread;
class RunLoop {
friend Thread<RunLoop>;
protected:
// These are called by the Thread<> wrapper.
RunLoop();
~RunLoop();
// Called by the Thread<> wrapper to start the loop. When you implement this
// method in a child class, you *must* call this function as the last action.
void start();
protected:
// Called by the Thread<> wrapper to terminate this loop.
void stop();
private:
// Invokes function in the run loop.
void process();
public:
// Schedules a function to be executed as part of this run loop.
void invoke(std::function<void()> fn);
private:
const std::unique_ptr<uv::loop> runloop;
const std::unique_ptr<uv::async> runloopAsync;
std::mutex runloopMutex;
std::queue<std::function<void()>> runloopQueue;
};
}
}
#endif