diff options
author | rglarix <rglarix@users.noreply.github.com> | 2020-10-07 18:34:51 +0200 |
---|---|---|
committer | Jens Geyer <jensg@apache.org> | 2020-12-08 22:19:33 +0100 |
commit | 10f2556733b8ee34f2c3695f09c1dff717fdafc8 (patch) | |
tree | 5c6f83b0f216a167961b787940a35455ce506dcb | |
parent | fb4b5aa17b0542f6f75d9744ecfe904f57431331 (diff) | |
download | thrift-10f2556733b8ee34f2c3695f09c1dff717fdafc8.tar.gz |
THRIFT-5295 makeThread and ThreadFactory extensible
Client: cpp
Patch: Riccardo Ghetta
This closes #2260
Signed-off-by: rglarix <rglarix@users.noreply.github.com>
-rw-r--r-- | lib/cpp/src/thrift/concurrency/Thread.h | 17 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/ThreadFactory.h | 6 |
2 files changed, 15 insertions, 8 deletions
diff --git a/lib/cpp/src/thrift/concurrency/Thread.h b/lib/cpp/src/thrift/concurrency/Thread.h index ed43754df..344d2ca31 100644 --- a/lib/cpp/src/thrift/concurrency/Thread.h +++ b/lib/cpp/src/thrift/concurrency/Thread.h @@ -67,10 +67,11 @@ private: * * @see apache::thrift::concurrency::ThreadFactory) */ -class Thread final : public std::enable_shared_from_this<Thread> { +class Thread : public std::enable_shared_from_this<Thread> { public: typedef std::thread::id id_t; + typedef void (*thread_funct_t)(std::shared_ptr<Thread> ); enum STATE { uninitialized, starting, started, stopping, stopped }; @@ -84,7 +85,7 @@ public: this->_runnable = runnable; } - ~Thread() { + virtual ~Thread() { if (!detached_ && thread_->joinable()) { try { join(); @@ -117,7 +118,7 @@ public: * configuration then invokes the run method of the Runnable object bound * to this thread. */ - void start() { + virtual void start() { if (getState() != uninitialized) { return; } @@ -126,7 +127,7 @@ public: setState(starting); Synchronized sync(monitor_); - thread_ = std::unique_ptr<std::thread>(new std::thread(threadMain, selfRef)); + thread_ = std::unique_ptr<std::thread>(new std::thread(getThreadFunc(), selfRef)); if (detached_) thread_->detach(); @@ -142,7 +143,7 @@ public: * until this thread completes. If the target thread is not joinable, then * nothing happens. */ - void join() { + virtual void join() { if (!detached_ && state_ != uninitialized) { thread_->join(); } @@ -158,6 +159,12 @@ public: */ std::shared_ptr<Runnable> runnable() const { return _runnable; } +protected: + + virtual thread_funct_t getThreadFunc() const { + return threadMain; + } + private: std::shared_ptr<Runnable> _runnable; std::unique_ptr<std::thread> thread_; diff --git a/lib/cpp/src/thrift/concurrency/ThreadFactory.h b/lib/cpp/src/thrift/concurrency/ThreadFactory.h index a1547a6e0..9db832e32 100644 --- a/lib/cpp/src/thrift/concurrency/ThreadFactory.h +++ b/lib/cpp/src/thrift/concurrency/ThreadFactory.h @@ -31,7 +31,7 @@ namespace concurrency { * Factory to create thread object and bind them to Runnable * object for execution */ -class ThreadFactory final { +class ThreadFactory { public: /** * All threads created by a factory are reference-counted @@ -43,7 +43,7 @@ public: */ ThreadFactory(bool detached = true) : detached_(detached) { } - ~ThreadFactory() = default; + virtual ~ThreadFactory() = default; /** * Gets current detached mode @@ -58,7 +58,7 @@ public: /** * Create a new thread. */ - std::shared_ptr<Thread> newThread(std::shared_ptr<Runnable> runnable) const; + virtual std::shared_ptr<Thread> newThread(std::shared_ptr<Runnable> runnable) const; /** * Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread |