Skip to content

Simplified enqueue #4

Closed
Closed

Description

@ltwardus

I've simplified the enqueue method, now the additional classes are not needed and the container is std::deque<std::function<void()>>:

template<class Result, class Callable>
std::future<Result> ThreadPoolManager::enqueue(Callable callable) {
  /**
   * Create task, we use RAII here and copy-capture lambda later as this std::packed_task has to be
   * deleted after it was executed.
   */
  auto task = std::make_shared<std::packaged_task<Result()>>(callable);

  /** Get the result of the task */
  std::future<Result> result = task->get_future();

  {
    /** Lock the queue */
    std::unique_lock<Mutex> lock(mutex_);

    /** Push new task to the queue */
    tasks_.push_back([=] {
      /** Execute task */
      (*task)();
    });
  }
  /** Notify one worker that there is a work to do */
  conditional_variable_.notify_one();

  return result;
}

Activity

  1. progschj commented on Feb 16, 2013

    @progschj
    Owner

    I like this. If only there was a way to move-capture then one could even avoid the shared_ptr.

  2. ltwardus commented on Feb 17, 2013

    @ltwardus
    Author

    Well, i was messing with something like:

      std::packaged_task<Result()> task(callable);
    
      ...
    
      tasks_.push_back(std::bind([](std::packaged_task<Result()> &&task){task();}, std::move(task)));
    
    ...

    , but with no luck and at the moment it already looks too weird and in my opinion is complicating things more than it's needed :)

  3. added a commit that references this issue on Feb 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions