A wrapper for system threads API. Thread class manages lifetime of a running IRoutine and guarantees that it will be possible to access the IRoutine after Thread::Create() call until cancellation or destruction. In the latter case, system thread will be responsible for deletion of a IRoutine.
| 25 | /// latter case, system thread will be responsible for deletion of a |
| 26 | /// IRoutine. |
| 27 | class Thread |
| 28 | { |
| 29 | std::thread m_thread; |
| 30 | std::shared_ptr<IRoutine> m_routine; |
| 31 | |
| 32 | DISALLOW_COPY(Thread); |
| 33 | |
| 34 | public: |
| 35 | Thread(); |
| 36 | ~Thread(); |
| 37 | |
| 38 | /// Run thread immediately. |
| 39 | /// |
| 40 | /// @param routine Routine that will be executed on m_thread and |
| 41 | /// destroyed by the current Thread instance or by |
| 42 | /// the m_thread, if it is detached during the |
| 43 | /// execution of routine. |
| 44 | bool Create(std::unique_ptr<IRoutine> && routine); |
| 45 | |
| 46 | /// Calling the IRoutine::Cancel method, and Join'ing with the task |
| 47 | /// execution. After that, routine is deleted. |
| 48 | void Cancel(); |
| 49 | |
| 50 | /// Wait for thread ending. |
| 51 | void Join(); |
| 52 | |
| 53 | /// \return Pointer to the routine. |
| 54 | IRoutine * GetRoutine(); |
| 55 | |
| 56 | /// \return Pointer to the routine converted to T *. When it's not |
| 57 | /// possible to convert routine to the T *, release version |
| 58 | /// returns nullptr, debug version fails. |
| 59 | template <typename T> |
| 60 | T * GetRoutineAs() |
| 61 | { |
| 62 | ASSERT(m_routine.get(), ("Routine is not set")); |
| 63 | T * ptr = dynamic_cast<T *>(m_routine.get()); |
| 64 | ASSERT(ptr, ("Can't convert IRoutine* to", TO_STRING(T) "*")); |
| 65 | return ptr; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | /// Suspends the execution of the current thread until the time-out interval elapses. |
| 70 | /// @param[in] ms time-out interval in milliseconds |
no outgoing calls
no test coverage detected