Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Bug in TBB With ref_count() ?

AJ13
New Contributor I
589 Views
I'm really not sure if this is a bug or not, it seems to be. The following code causes an assertion (compiled without optimizations):

Assertion 1L<state() & (1L<<:ALLOCATED>Aborted

Here's the code:
#include
#include

#include

class HelloTask : public tbb::task
{
public:
HelloTask(int depth) : _depth(depth) { }
tbb::task* execute()
{
if(_depth == 0) return 0;

HelloTask& a = *new ( allocate_child() ) HelloTask(_depth - 1);
HelloTask& b = *new ( allocate_child() ) HelloTask(_depth - 1);

set_ref_count(ref_count() + 1);
spawn(a);
set_ref_count(ref_count() + 1);
spawn(b);

int tmp;
for(int i = 0; i < 1000000; ++i)
{
tmp = tmp*i;
}

set_ref_count(ref_count() + 1);
wait_for_all();

return 0;
}
private:
int _depth;

};


int main()
{
tbb::task_scheduler_init init;

HelloTask& a = *new ( tbb::task::allocate_root() ) HelloTask(10);
tbb::task::spawn_root_and_wait(a);

}

0 Kudos
2 Replies
RafSchietekat
Valued Contributor III
589 Views
Once you spawn a child, you must not use set_ref_count() again; use allocate_additional_child_of() instead.
0 Kudos
AJ13
New Contributor I
589 Views
Quoting - Raf Schietekat
Once you spawn a child, you must not use set_ref_count() again; use allocate_additional_child_of() instead.

Aha, thanks.
0 Kudos
Reply