Skip to content
Snippets Groups Projects
Commit 9c256980 authored by Sree Kuchibhotla's avatar Sree Kuchibhotla
Browse files

Merge pull request #4192 from ctiller/stupid-threads

Two argument variant for grpc::thread
parents ceb7318d 52a2ebad
No related branches found
No related tags found
No related merge requests found
...@@ -46,10 +46,21 @@ class thread { ...@@ -46,10 +46,21 @@ class thread {
joined_ = false; joined_ = false;
start(); start();
} }
template <class T, class U>
thread(void (T::*fptr)(U arg), T *obj, U arg) {
func_ = new thread_function_arg<T, U>(fptr, obj, arg);
joined_ = false;
start();
}
~thread() { ~thread() {
if (!joined_) std::terminate(); if (!joined_) std::terminate();
delete func_; delete func_;
} }
thread(thread &&other)
: func_(other.func_), thd_(other.thd_), joined_(other.joined_) {
other.joined_ = true;
other.func_ = NULL;
}
void join() { void join() {
gpr_thd_join(thd_); gpr_thd_join(thd_);
joined_ = true; joined_ = true;
...@@ -80,6 +91,18 @@ class thread { ...@@ -80,6 +91,18 @@ class thread {
void (T::*fptr_)(); void (T::*fptr_)();
T *obj_; T *obj_;
}; };
template <class T, class U>
class thread_function_arg : public thread_function_base {
public:
thread_function_arg(void (T::*fptr)(U arg), T *obj, U arg)
: fptr_(fptr), obj_(obj), arg_(arg) {}
virtual void call() { (obj_->*fptr_)(arg_); }
private:
void (T::*fptr_)(U arg);
T *obj_;
U arg_;
};
thread_function_base *func_; thread_function_base *func_;
gpr_thd_id thd_; gpr_thd_id thd_;
bool joined_; bool joined_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment