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

Tasks async / detached ? And no execution of task on a single core machine ?

hexa00
Beginner
678 Views
Hi,
I would like the task to run and then delete itself without having to explicitly wait for it from another thread or keep a reference to it in my code. Is it possible ? I'm having problems with for example this code :
[cpp]#include 
#include 

/*
export LD_LIBRARY_PATH="/opt/tbb30_20110704oss/lib/:/usr/lib"

g++ -L/opt/bb30_20110704oss/lib -ltbb -I/opt/tbb/tbb30_20110704oss/include main.cc -I/usr/include -L/usr/lib
*/

class MyTask: public tbb::task {

public:
  MyTask() {}
  ~MyTask() {}
   tbb::task* execute() {
      std::cout << "Task execute " << std::endl;
      sleep(1);
      std::cout << "Task execute end" << std::endl;
  }
};

void create_task()
{
  MyTask& task = *new(tbb::task::allocate_root()) MyTask();
  std::cout << "Task spawning task" << std::endl;
  tbb::task::spawn(task);
  std::cout << "Task create sleep" << std::endl;
  sleep(10);
  std::cout << "Task create sleep end" << std::endl;
}

int main(int ac, char* av[])
{
  create_task();
  std::cout << "Main sleep start" << std::endl;
  sleep(10);
  /// Wait for all ??
}[/cpp]
On a dual core I will get :
./a.out
Task spawning task
Task create sleep
Task execute
Task execute end
Segmentation fault (core dumped)

On :
0x00007ffff7bcb6a3 in tbb::internal::custom_scheduler<:INTERNAL::INTELSCHEDULERTRAITS>::receive_or_steal_task(long&, bool) ()
from /opt//tbb/tbb30_20110704oss/lib/libtbb.so.2
(gdb) bt
#0 0x00007ffff7bcb6a3 in tbb::internal::custom_scheduler<:INTERNAL::INTELSCHEDULERTRAITS>::receive_or_steal_task(long&, bool)
() from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#1 0x00007ffff7bc810d in tbb::internal::arena::process(tbb::internal::generic_scheduler&) ()
from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#2 0x00007ffff7bc7a6b in tbb::internal::market::process(rml::job&) () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#3 0x00007ffff7bc5238 in tbb::internal::rml::private_worker::run() () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#4 0x00007ffff7bc5409 in tbb::internal::rml::private_worker::thread_routine(void*) ()
from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#5 0x00007ffff6c5cd8c in start_thread (arg=0x7ffff6030700) at pthread_create.c:304
#6 0x00007ffff715e04d in clone () at ../sysdeps/unix/sysv/linux/

And on a single core the task is just not executed :

./a.out
Task spawning task
Task create sleep
Task create sleep end
Main sleep start

No crash...

I'm really missing something here, if anyone would like to explain It would be apreciated ...

Thank you!
0 Kudos
2 Replies
Alexey-Kukanov
Employee
678 Views
For such "fire-and-forget" scenario, use tbb::task::enqueue(your_task).
0 Kudos
hexa00
Beginner
678 Views
Indeed thanks! Seems my reference doc was outdated...

But I still get the crash at the end in both cases dual & single core.. any ideas on this : ? (Same code but with ::enqueue)

WIth :tbb30_20110704oss on Ubuntu gcc 4.3 64bit...

0x00007fe3f2344379 in tbb::internal::market::insert_arena_into_list () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
Current language: auto; currently asm
(gdb) bt
#0 0x00007fe3f2344379 in tbb::internal::market::insert_arena_into_list () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#1 0x00007fe3f2344642 in tbb::internal::market::update_arena_top_priority () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#2 0x00007fe3f2344e6a in tbb::internal::market::update_arena_priority () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#3 0x00007fe3f2349fc0 in tbb::internal::custom_scheduler<:INTERNAL::INTELSCHEDULERTRAITS>::local_wait_for_all ()
from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#4 0x00007fe3f2345f5a in tbb::internal::arena::process () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#5 0x00007fe3f234525b in tbb::internal::market::process () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#6 0x00007fe3f2342406 in tbb::internal::rml::private_worker::run () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#7 0x00007fe3f23425c9 in tbb::internal::rml::private_worker::thread_routine () from /opt/tbb/tbb30_20110704oss/lib/libtbb.so.2
#8 0x00007fe3f1416fc7 in start_thread () from /lib/libpthread.so.0
#9 0x00007fe3f18ff64d in clone () from /lib/libc.so.6
#10 0x0000000000000000 in ?? ()
0 Kudos
Reply