reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 1706476c5af5ecd1b28df14a40c4fbc284505f0b
parent 76d1541332699a5f3525aeae11bc2ce8c13dd4a3
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Tue, 21 Nov 2017 02:14:23 -0500

thread: use an instance method for the thread procedure and small code cleanup

Diffstat:
Msrc/richedit-win32.cpp | 4++--
Msrc/thread.cpp | 19+++++++++----------
Msrc/thread.hpp | 2+-
3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/richedit-win32.cpp b/src/richedit-win32.cpp @@ -82,10 +82,10 @@ bool RichEdit::setRichText(const string &rtf) EDITSTREAM es{}; es.dwCookie = (DWORD_PTR)&stream; - es.pfnCallback = [](DWORD_PTR cookie, LPBYTE buf, LONG size, LONG *pcb) { + es.pfnCallback = [](DWORD_PTR cookie, LPBYTE buf, LONG size, LONG *pcb) -> DWORD { stringstream *stream = reinterpret_cast<stringstream *>(cookie); *pcb = (LONG)stream->readsome((char *)buf, size); - return (DWORD)0; + return 0; }; SendMessage(handle(), EM_STREAMIN, SF_RTF, (LPARAM)&es); diff --git a/src/thread.cpp b/src/thread.cpp @@ -88,7 +88,10 @@ void ThreadTask::exec() WorkerThread::WorkerThread() : m_exit(false) { m_wake = CreateEvent(nullptr, true, false, nullptr); - m_thread = CreateThread(nullptr, 0, run, (void *)this, 0, nullptr); + m_thread = CreateThread(nullptr, 0, [](void *ptr) -> DWORD { + static_cast<WorkerThread *>(ptr)->run(); + return 0; + }, static_cast<void *>(this), 0, nullptr); } WorkerThread::~WorkerThread() @@ -102,25 +105,21 @@ WorkerThread::~WorkerThread() CloseHandle(m_thread); } -DWORD WINAPI WorkerThread::run(void *ptr) +void WorkerThread::run() { - WorkerThread *thread = static_cast<WorkerThread *>(ptr); - DownloadContext context; - while(!thread->m_exit) { - while(ThreadTask *task = thread->nextTask()) { + while(!m_exit) { + while(ThreadTask *task = nextTask()) { if(auto dl = dynamic_cast<Download *>(task)) dl->setContext(&context); task->exec(); } - ResetEvent(thread->m_wake); - WaitForSingleObject(thread->m_wake, INFINITE); + ResetEvent(m_wake); + WaitForSingleObject(m_wake, INFINITE); } - - return 0; } ThreadTask *WorkerThread::nextTask() diff --git a/src/thread.hpp b/src/thread.hpp @@ -91,7 +91,7 @@ public: void clear(); private: - static DWORD WINAPI run(void *); + void run(); ThreadTask *nextTask(); HANDLE m_wake;