Avoid unecessary object copies

This commit is contained in:
Benoit Steiner 2016-08-01 17:03:39 -07:00
parent 7995cec90c
commit e256acec7c
2 changed files with 6 additions and 10 deletions

View File

@ -151,9 +151,7 @@ struct ThreadPoolDevice {
template <class Function, class... Args> template <class Function, class... Args>
EIGEN_STRONG_INLINE Notification* enqueue(Function&& f, Args&&... args) const { EIGEN_STRONG_INLINE Notification* enqueue(Function&& f, Args&&... args) const {
Notification* n = new Notification(); Notification* n = new Notification();
std::function<void()> func = pool_->Schedule(std::bind(&FunctionWrapperWithNotification<Function, Args...>::run, n, f, args...));
std::bind(&FunctionWrapperWithNotification<Function, Args...>::run, n, f, args...);
pool_->Schedule(func);
return n; return n;
} }
@ -161,15 +159,13 @@ struct ThreadPoolDevice {
EIGEN_STRONG_INLINE void enqueue_with_barrier(Barrier* b, EIGEN_STRONG_INLINE void enqueue_with_barrier(Barrier* b,
Function&& f, Function&& f,
Args&&... args) const { Args&&... args) const {
std::function<void()> func = std::bind( pool_->Schedule(std::bind(
&FunctionWrapperWithBarrier<Function, Args...>::run, b, f, args...); &FunctionWrapperWithBarrier<Function, Args...>::run, b, f, args...));
pool_->Schedule(func);
} }
template <class Function, class... Args> template <class Function, class... Args>
EIGEN_STRONG_INLINE void enqueueNoNotification(Function&& f, Args&&... args) const { EIGEN_STRONG_INLINE void enqueueNoNotification(Function&& f, Args&&... args) const {
std::function<void()> func = std::bind(f, args...); pool_->Schedule(std::bind(f, args...));
pool_->Schedule(func);
} }
// Returns a logical thread index between 0 and pool_->NumThreads() - 1 if // Returns a logical thread index between 0 and pool_->NumThreads() - 1 if

View File

@ -21,14 +21,14 @@ struct StlThreadEnvironment {
// destructor must join the thread. // destructor must join the thread.
class EnvThread { class EnvThread {
public: public:
EnvThread(std::function<void()> f) : thr_(f) {} EnvThread(std::function<void()> f) : thr_(std::move(f)) {}
~EnvThread() { thr_.join(); } ~EnvThread() { thr_.join(); }
private: private:
std::thread thr_; std::thread thr_;
}; };
EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(f); } EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(std::move(f)); }
Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; } Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; }
void ExecuteTask(const Task& t) { t.f(); } void ExecuteTask(const Task& t) { t.f(); }
}; };