@@ -16,7 +16,7 @@ using namespace dmq;
1616// Thread
1717// ----------------------------------------------------------------------------
1818Thread::Thread (const std::string& threadName, size_t maxQueueSize)
19- : m_thread(nullptr )
19+ : m_thread(std:: nullopt )
2020 , m_exit(false )
2121 , THREAD_NAME(threadName)
2222 , MAX_QUEUE_SIZE(maxQueueSize)
@@ -38,17 +38,17 @@ bool Thread::CreateThread(std::optional<dmq::Duration> watchdogTimeout)
3838{
3939 if (!m_thread)
4040 {
41- m_threadStartPromise = std::promise< void > ();
42- m_threadStartFuture = m_threadStartPromise. get_future ();
41+ m_threadStartPromise. emplace ();
42+ m_threadStartFuture. emplace (m_threadStartPromise-> get_future () );
4343 m_exit = false ;
4444
45- m_thread = std::unique_ptr<std::thread>( new thread ( &Thread::Process, this ) );
45+ m_thread. emplace ( &Thread::Process, this );
4646
4747 auto handle = m_thread->native_handle ();
4848 SetThreadName (handle, THREAD_NAME);
4949
5050 // Wait for the thread to enter the Process method
51- m_threadStartFuture. get ();
51+ m_threadStartFuture-> get ();
5252
5353 m_lastAliveTime.store (Timer::GetNow ());
5454
@@ -59,12 +59,12 @@ bool Thread::CreateThread(std::optional<dmq::Duration> watchdogTimeout)
5959 m_watchdogTimeout = watchdogTimeout.value ();
6060
6161 // Timer to ensure the Thread instance runs periodically.
62- m_threadTimer = std::make_unique <Timer>();
62+ m_threadTimer = std::unique_ptr <Timer>(new Timer () );
6363 m_threadTimerConn = m_threadTimer->OnExpired .Connect (MakeDelegate (this , &Thread::ThreadCheck, *this ));
6464 m_threadTimer->Start (m_watchdogTimeout.load () / 4 );
6565
6666 // Timer to check that this Thread instance runs.
67- m_watchdogTimer = std::make_unique <Timer>();
67+ m_watchdogTimer = std::unique_ptr <Timer>(new Timer () );
6868 m_watchdogTimerConn = m_watchdogTimer->OnExpired .Connect (MakeDelegate (this , &Thread::WatchdogCheck));
6969 m_watchdogTimer->Start (m_watchdogTimeout.load () / 2 );
7070 }
@@ -79,7 +79,7 @@ bool Thread::CreateThread(std::optional<dmq::Duration> watchdogTimeout)
7979// ----------------------------------------------------------------------------
8080std::thread::id Thread::GetThreadId ()
8181{
82- if (m_thread == nullptr )
82+ if (! m_thread. has_value () )
8383 throw std::invalid_argument (" Thread pointer is null" );
8484
8585 return m_thread->get_id ();
@@ -147,7 +147,7 @@ void Thread::ExitThread()
147147 }
148148
149149 // Create a new ThreadMsg
150- std::shared_ptr<ThreadMsg> threadMsg ( new ThreadMsg (MSG_EXIT_THREAD, 0 ) );
150+ auto threadMsg = xmake_shared< ThreadMsg> (MSG_EXIT_THREAD, nullptr );
151151
152152 {
153153 lock_guard<mutex> lock (m_mutex);
@@ -182,7 +182,7 @@ void Thread::ExitThread()
182182
183183 {
184184 lock_guard<mutex> lock (m_mutex);
185- m_thread = nullptr ;
185+ m_thread. reset () ;
186186 while (!m_queue.empty ())
187187 m_queue.pop ();
188188
@@ -202,11 +202,11 @@ void Thread::DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg)
202202 if (m_exit.load ())
203203 return ;
204204
205- if (m_thread == nullptr )
205+ if (! m_thread. has_value () )
206206 throw std::invalid_argument (" Thread pointer is null" );
207207
208208 // If using XALLOCATOR explicit operator new required. See xallocator.h.
209- std::shared_ptr<ThreadMsg> threadMsg ( new ThreadMsg (MSG_DISPATCH_DELEGATE, msg) );
209+ auto threadMsg = xmake_shared< ThreadMsg> (MSG_DISPATCH_DELEGATE, msg);
210210
211211 std::unique_lock<std::mutex> lk (m_mutex);
212212
@@ -264,7 +264,7 @@ void Thread::ThreadCheck()
264264void Thread::Process ()
265265{
266266 // Signal that the thread has started processing to notify CreateThread
267- m_threadStartPromise. set_value ();
267+ m_threadStartPromise-> set_value ();
268268
269269 LOG_INFO (" Thread::Process Start {}" , THREAD_NAME);
270270
0 commit comments