Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
9 Views

Cannot Return Data From Task

Jump to solution

Hi,

This is a meaningless example for testing purposes.  I was just trying to figure out how to make a task populate a container passed by reference from another location.  I haven't had much luck...

#include "tbb/task.h"

#include <functional>
#include <random>

#define NUM_POINTS 100000


class TASK_generateRandomNumbers : public tbb::task
{
public:
   TASK_generateRandomNumbers(std::function<void(std::vector<double>)> fnc, std::vector<double>& numbers) : _fnc(fnc), _numbers(numbers) {}
   ~TASK_generateRandomNumbers() {};

   tbb::task* execute()
   {
      _fnc(_numbers);
      return nullptr;
   }

private:
   std::function<void(std::vector<double>)> _fnc;
   std::vector<double> _numbers;
};


class Manager
{
public:
   Manager() { _numbers.reserve(NUM_POINTS); }
   ~Manager() {}

   void GenerateNumbers()
   {
      auto fn = std::bind(&Manager::generateRandomNumbers, this, _numbers);
      TASK_generateRandomNumbers* t = new(tbb::task::allocate_root()) TASK_generateRandomNumbers(fn, _numbers);
      tbb::task::spawn_root_and_wait(*t);
   }

   auto GetNumbers() const { return _numbers; }

private:
   void generateRandomNumbers(std::vector<double>& numbers)
   {
      std::mt19937_64 gen;
      std::uniform_real_distribution<double> dis(0.0, 1000.0);

      for (size_t i = 0; i < NUM_POINTS; i++)
      {
         numbers.push_back(dis(gen));
      }
   }   
   
   std::vector<double> _numbers;
};


int main()
{
   Manager mgr;
   mgr.GenerateNumbers();
   auto numbers = mgr.GetNumbers();
}

 

I see the random numbers being generated and inserted into the vector but when the method returns, the vector in the task's execute method is empty.  

Is it possible to use tasks to populate a container that is passed around?

Thanks.

0 Kudos

Accepted Solutions
Highlighted
New Contributor I
9 Views

I forgot that if I want to pass a reference to std::bind, I need to use std::ref (line 35).

View solution in original post

0 Kudos
2 Replies
Highlighted
New Contributor I
10 Views

I forgot that if I want to pass a reference to std::bind, I need to use std::ref (line 35).

View solution in original post

0 Kudos
Highlighted
Employee
9 Views

In addition, pay attention that TASK_generateRandomNumbers has its own copy of std::vector (it is not a reference). Moreover, any arguments passed to _fnc call are ignored because the bind does not use any placeholders. So the vector is always empty inside the task.

Regards,
Alex

0 Kudos