You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2020/12/08 21:19:56 UTC
[thrift] branch master updated: THRIFT-5295 makeThread and
ThreadFactory extensible Client: cpp Patch: Riccardo Ghetta
This is an automated email from the ASF dual-hosted git repository.
jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push:
new 10f2556 THRIFT-5295 makeThread and ThreadFactory extensible Client: cpp Patch: Riccardo Ghetta
10f2556 is described below
commit 10f2556733b8ee34f2c3695f09c1dff717fdafc8
Author: rglarix <rg...@users.noreply.github.com>
AuthorDate: Wed Oct 7 18:34:51 2020 +0200
THRIFT-5295 makeThread and ThreadFactory extensible
Client: cpp
Patch: Riccardo Ghetta
This closes #2260
Signed-off-by: rglarix <rg...@users.noreply.github.com>
---
lib/cpp/src/thrift/concurrency/Thread.h | 17 ++++++++++++-----
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 ed43754..344d2ca 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 a1547a6..9db832e 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