TizenRefApp-8204 [Gallery] Redesign ucl::Thread implementation to avoid crashes 26/119926/1
authorIgor Nazarov <i.nazarov@samsung.com>
Mon, 20 Mar 2017 11:14:52 +0000 (13:14 +0200)
committerIgor Nazarov <i.nazarov@samsung.com>
Mon, 20 Mar 2017 14:27:33 +0000 (16:27 +0200)
Change-Id: I376f7b5655618a0c88c8f410b2e3281f35cb4847

ucl/inc/ucl/util/threading/Thread.h
ucl/inc/ucl/util/threading/Thread.hpp
ucl/inc/ucl/util/types/baseTypes.h

index aad86e8852de33f9a4e0aab7e65b5ecc416a6587..08399217281c80d25c0d0c3d1ff4137e34d4a276 100644 (file)
 
 namespace ucl {
 
-       class Thread : public NonCopyable {
+       class Thread final : public NonCopyable {
        public:
-               explicit Thread(bool createSuspended = false);
-               virtual ~Thread();
+               Thread();
+               template <class FUNC>
+               explicit Thread(FUNC &&func);
+               ~Thread();
+
                bool wasStarted() const;
                bool wasJoinded() const;
-               bool start();
+
+               template <class FUNC>
+               bool start(FUNC &&func);
                void join();
+
                pthread_t *getHandle();
-       protected:
-               virtual void execute() = 0;
+
        private:
+               std::function<void()> m_func;
                pthread_t m_thread;
                bool m_wasStarted;
                bool m_wasJoined;
index f3d7629133d486d007149a730635e311784cfcb0..74259c15180d96b255f6f4fdbea141414178e751 100644 (file)
 
 namespace ucl {
 
-       inline Thread::Thread(bool createSuspended) :
+       inline Thread::Thread() :
                m_thread(),
                m_wasStarted(false),
                m_wasJoined(false)
        {
-               if (!createSuspended) {
-                       start();
-               }
+       }
+
+       template <class FUNC>
+       inline Thread::Thread(FUNC &&func) :
+               Thread()
+       {
+               start(func);
        }
 
        inline Thread::~Thread()
@@ -45,21 +49,24 @@ namespace ucl {
                return m_wasJoined;
        }
 
-       inline bool Thread::start()
+       template <class FUNC>
+       inline bool Thread::start(FUNC &&func)
        {
                if (m_wasStarted) {
                        UCL_WLOG("Already started!");
                        return false;
                }
+               m_func = std::forward<FUNC>(func);
                const int res = pthread_create(&m_thread, NULL,
                        [](void *data) -> void *
                        {
-                               static_cast<Thread *>(data)->execute();
+                               static_cast<Thread *>(data)->m_func();
                                return nullptr;
                        },
                        this);
                if (res != 0) {
                        UCL_ELOG("pthread_create() failed: %d", res);
+                       m_func = {};
                        return false;
                }
                m_wasStarted = true;
@@ -78,6 +85,7 @@ namespace ucl {
                }
                m_wasJoined = true;
                pthread_join(m_thread, NULL);
+               m_func = {};
        }
 
        inline pthread_t *Thread::getHandle()
index 0d224279a63f0e87f19af23e2839f5c8bad62d91..5b280de6819a865081366583f89909566184d5f9 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <string>
 #include <memory>
+#include <functional>
 #include <type_traits>
 #include <utility>