0

this question is similar to How to get return value from a function called which executes in another thread in TBB? but I am working with tasks.

I have the following code:

vector<vector<int> > all;
for(h=0; h<100;h++){
    vector<int> vector1= Node(anotherVector[h], value - 1, anotherVector[h].size()); 
    for (unsigned int u = 0; u < vector1.size(); u++) {
        all[h].push_back(vector1[u]);
    }
}

"vector1" saves the result of the recursive call from Node. I want TBB to call the Node parallel. And then to use "vector1" to push it to "all". My problem is that if I am trying to use tasks, I need to return a Task object from Node in order to use spawn_and_wait_for_all(taskobjectlist here).

Thanks in advance for any help.

Community
  • 1
  • 1
Christian
  • 25
  • 8

1 Answers1

3

This is not a direct answer to your question because as it is stated it makes less sense to me. So, a few notes why.

tbb::task is a low-level interface which is not generally recommended for usage unless you know what you are doing. For example, spawn_and_wait_for_all(task_list) is inefficient for long lists (>10). Recursive parallelism like divide and conquire, and especially a work with vectors, can be implemented using tbb::parallel_reduce or if you want use just tasks, take a look at tbb::task_group or tbb::parallel_invoke. Please also note that std:vector does not allow performing safe concurrent push_back() simultaneously from different tasks, it is not thread-safe. Instead, you can try tbb::concurrent_vector<> but I'd rather recommend to try tbb::parallel_reduce or tbb::combinable first.

Anton
  • 6,349
  • 1
  • 25
  • 53
  • Hi Anton, thanks for the advice. I looked a bit further into this but can´t really find something that is working. For example when I use task_groups, the code could be like: g->run(Node(anotherVector[h], value - 1, anotherVector[h].size())); g->wait(); I have no option to get the returned result of that task. – Christian Jan 03 '16 at 11:43
  • A task can carry a reference to the output structure. No need to return something like in functional programming. – Anton Jan 03 '16 at 15:32
  • Where can I read more about this? Is there an example somewhere? Thanks! – Christian Jan 04 '16 at 12:15
  • I need a variable that only the upper task / thread can access, not a single global variable... – Christian Jan 04 '16 at 12:26
  • 1
    well.. You can read C++ language reference/books for how to create classes with your variables. Or using new C++ standard (2011), you can use lambda functions (which result in the same auto-generated class) and capture the pointer/reference to your output structure. – Anton Jan 04 '16 at 16:50