summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-04-10 15:52:02 +0200
committerKonstantin Käfer <mail@kkaefer.com>2015-04-10 15:52:02 +0200
commitbd531b9e080f3300c6947b0e0e515218bb395f45 (patch)
tree1f2855a2ba6216e35a81d51f55e37d8a6e28e98c /include
parentbba3d9838a512c75a63f5eb9263913ec96011e8d (diff)
downloadqtlocation-mapboxgl-bd531b9e080f3300c6947b0e0e515218bb395f45.tar.gz
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.
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/util/run_loop.hpp2
-rw-r--r--include/mbgl/util/thread.hpp11
2 files changed, 12 insertions, 1 deletions
diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp
index f0318d2026..5aefcd1d3e 100644
--- a/include/mbgl/util/run_loop.hpp
+++ b/include/mbgl/util/run_loop.hpp
@@ -49,4 +49,4 @@ private:
}
}
-#endif \ No newline at end of file
+#endif
diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp
index 943039c5d6..6983b81907 100644
--- a/include/mbgl/util/thread.hpp
+++ b/include/mbgl/util/thread.hpp
@@ -7,6 +7,14 @@
namespace mbgl {
namespace util {
+// Manages a thread with Object.
+
+// Upon creation of this object, it launches a thread, creates an object of type Object in that
+// thread, and then calls .start(); on that object. When the Thread<> object is destructed, the
+// Object's .stop() function is called, and the destructor waits for thread termination. The
+// Thread<> constructor blocks until the thread and the Object are fully created, so after the
+// object creation, it's safe to obtain the Object stored in this thread.
+
template <class Object>
class Thread {
public:
@@ -23,6 +31,7 @@ public:
private:
std::thread thread;
+ std::promise<void> joinable;
Object& object;
};
@@ -35,6 +44,7 @@ Thread<Object>::Thread(Args&&... args)
Object context(::std::forward<Args>(args)...);
promise.set_value(context);
context.start();
+ joinable.get_future().get();
});
return promise.get_future().get();
}()) {
@@ -43,6 +53,7 @@ Thread<Object>::Thread(Args&&... args)
template <class Object>
Thread<Object>::~Thread() {
object.stop();
+ joinable.set_value();
thread.join();
}