Question about TBB / C ++ code

I am reading a block block. I do not understand this piece of code:

            FibTask& a=*new(allocate_child()) FibTask(n-1,&x);
FibTask& b=*new(allocate_child()) FibTask(n-2,&y);

What does this directive mean? class object and new work together? Thanks for the explanation.

The following code is the protection of this FibTask class.

class FibTask: public task

{
public:

 const long n;
    long* const sum;
 FibTask(long n_,long* sum_):n(n_),sum(sum_)
 {}
 task* execute()
 {
  if(n<CutOff)
  {
   *sum=SFib(n);
  }
  else
  {
   long x,y;

   FibTask& a=*new(allocate_child()) FibTask(n-1,&x);
   FibTask& b=*new(allocate_child()) FibTask(n-2,&y);
   set_ref_count(3);
   spawn(b);
   spawn_and_wait_for_all(a);
   *sum=x+y;
  }
  return 0;

 }
};
+3
source share
2 answers
new(pointer) Type(arguments);

This syntax is called placing a new one , in which it is assumed that the location pointerhas already been allocated, then the constructor is Typesimply called at this place and returns a value Type*.

Then this is Type*dereferenced to give Type&.

new , , , (allocate_child()).

+4

   FibTask& a=*new(allocate_child()) FibTask(n-1,&x);
   FibTask& b=*new(allocate_child()) FibTask(n-2,&y);

. , , , thsi- > allocate_child() . , , . , , , .

   FibTask* a=new(allocate_child()) FibTask(n-1,&x);
   FibTask* b=new(allocate_child()) FibTask(n-2,&y);
   set_ref_count(3);
   spawn(*b);
   spawn_and_wait_for_all(*a);

, , , , , .

+4

Source: https://habr.com/ru/post/1743552/


All Articles