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

timed tasks?

alexander
Beginner
395 Views

Hi TBB Experts!

I am currently using some (allocators, containers, atomic functions etc..) of the TBB components in my open-source streaming server project (mammoth-project.org), and I'm very happy with that!

The server basically consists of two threadpools; one for io processing using boost::asio and one for heavier (non-blocking) protocol processing (video on demand for ex.). For the latter I have implemented 'timed-tasks', whichare simple tasks that are executed approximately afterx (0-????) amount ofms. Each task is able to requeue itsself and is executed when its timer elapses. For that I simply implemented amultiset (sorted on tasktimer)combined with a conditionand a mutexusing a 'timed_wait'.

Now I would like to replace my 'task scheduler' with the tbb scheduler. My question: is it possible (without having to dive into tbb task.cpp/task.hpp code) to implementmy 'timed tasks' (in a non-intrusive way) and use the queuing meschanism the tbb scheduler has? Orwould you advise me to keep my current task pool?

Basically; I would loveto do the following:
root->spawn(t, 100); //spawn task t after approximately 100ms.

I look forward to your response!
Alexander

0 Kudos
2 Replies
Dmitry_Vyukov
Valued Contributor I
395 Views

You can try something like the following. Create separate thread for management of timed tasks. Timed spawn will actually just send a message to that thread. The thread calculates the nearest time for firing and blocks until that time, when it wakes up it spawns normal TBB task.

Here is crude pseudo-code:

[cpp]message_queue q;

void timed_spawn(task* t, int delay)
{

t->set_affinity(my_thread_id);

q.enqueue(msg(t, delay));
}

void timed_thread_routine()

{

  spawn_root_and_wait(new timed_management_task());
}

void timed_management_task::execute()
{

  for (;;)

  {

    what = wait_for_something();

    if (what == fire_task)

    {

       t = get_next_task();

       spawn(t);

    }

    else if (what == new_incoming_message)

    {

       m = q.dequeue();

       remember_task_and_recalculate_next_fire_time(m->task, m->delay);

    }

    else if (what == terminate);

    {

      return;

    }

  }

}


[/cpp]

0 Kudos
RafSchietekat
Valued Contributor III
395 Views

"Or would you advise me to keep my current task pool?" Yes.

0 Kudos
Reply