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

pipeline with concurrent_queue

hitach__0
Beginner
540 Views
I am developing my project using pipeline, but i am having small issue.
My task is read a video and display every fram in sequence, that is i have to display image in every 30ms.
so i planed to use concurrent_queue to output buffer and display image it.
but i dont know how to create new thread within output buffer and hanle display image in every 30ms. I have to wake up that thread every 30ms and run. How to do that?
0 Kudos
14 Replies
Dmitry_Vyukov
Valued Contributor I
540 Views
[cpp]
volatile bool stop = false;
tbb::tbb_thread t (bind(thread_func, ref(stop)));
...
stop = true;
t.join();

void thread_func(bool volatile& stop)
{
  while (stop == false)
  {
    do_your_work_here();
	tbb::this_tbb_thread::sleep(tbb::tick_count::interval_t(0.03));
  }
}


[/cpp]

0 Kudos
hitach__0
Beginner
540 Views
thanks for quick reply,
can you explain how it works?
0 Kudos
Dmitry_Vyukov
Valued Contributor I
540 Views
[cpp]
int main()
{
	volatile bool stop = false;
	// this creates new thread (what you asked for)
	tbb::tbb_thread t (bind(thread_func, ref(stop)));
	...
	start pipeline here
	...
	// this terminates the thread
	stop = true;
	t.join();
}

// this a thread's function
void thread_func(bool volatile& stop)
{
  while (stop == false)
  {
    // here you must poll an image from the queue and display it
    do_your_work_here();
	// this suspends the thread for 30 ms
	tbb::this_tbb_thread::sleep(tbb::tick_count::interval_t(0.03));
  }
}


[/cpp]

0 Kudos
nikkey_c
Beginner
540 Views
Thanks I am also looking this.......
i think you miss something, isnt it?
tbb::tbb_threadt(bind(thread_func,ref(stop)));
what do u mean by ref?
0 Kudos
Dmitry_Vyukov
Valued Contributor I
540 Views
I had in mind bost::bind/ref. However the code is just typed in a text editor.
You can use whatever method you are comfortable with to create the functor. The simplest way is as follows:

struct my_thread
{
bool volatile& stop;
queue_t& queue;

my_thread(bool volatile& stop, queue_t& queue) : stop(stop), queue(queue) {}

void operator ()
{
// put your code here
}
};


tbb::tbb_thread t (my_thread(stop, queue));
0 Kudos
nikkey_c
Beginner
540 Views
This is the code i havewritten, but i coulndnt get the results, that i needed. It runs only my_thread function not InputFilterstage,TransformFilter stage and outputFilterstage.
class my_thread
{
public:
bool volatile& stop;
my_thread(bool volatile& st);
void operator ()();
};
my_thread::my_thread(bool volatile& st) : stop(st) {}
void my_thread::operator ()(){
while (stop == false)
{
printf("display \n"); // have to display image every 30ms. that means first 29 sec needed to do //other three stages job.
tbb::this_tbb_thread::sleep(tbb::tick_count::interval_t(3.));
}
}
//Outputbuffer of pipeline
void* MyOutputFilter::operator()( void* item ) {
MyBuffer& b = *static_cast(item);
//here b is ordered sequence of image frames.
//store those images into queue
volatile bool stop = false;
my_thread tt(stop);
//every 30th sec i have to run below thread and display image.
tbb::tbb_thread t (tt);
t.join();
return NULL;
}
(added) and i am using
static int NThread = tbb::task_scheduler_init::automatic;
for pipeline. I am developing code based on tbb's text_filter.cpp example
0 Kudos
RafSchietekat
Valued Contributor III
540 Views
You should never launch and join a thread from something done inside a task (MyOutputFilter::operator() is called for each data item): that's like the tail wagging the dog (always wanted to use that in a sentence). You could launch it outside the pipeline, and feed it work from the output stage through a concurrent_queue. As for sleep time, you'd probably want to check wall clock time first to compensate for various knowns and unknowns.
0 Kudos
hitach__0
Beginner
540 Views
sorry sir, i ddnt get your answer, ya we have to createmy_thread outside of pipeline, and pass it as argument.
But i dont know where I have to run this code:
tbb::tbb_thread t (tt);
t.join
and concurrent_queue?
can you send small code for that?
(added)
My task is run a function every 30ms insideMyOutputFilter. that function pops data from concurrent_queue.
0 Kudos
hitach__0
Beginner
540 Views
I think this code is for, after pipeline is finished.
But i want to run thread_func while pipeline s runing
0 Kudos
RafSchietekat
Valued Contributor III
540 Views
"But i dont know where I have to run this code:"
The constructor before you run the pipeline, the join() after you stop the pipeline? BTW, you should use an atomic to communicate between threads: using volatile is compiler-specific.
0 Kudos
hitach__0
Beginner
540 Views
Thank, my code works well.
even thoughi follow above code, i couldnot able to get sequence output.
void my_thread::operator ()(){
cvNamedWindow("tacka", CV_WINDOW_AUTOSIZE);
while (stop == false)
{
IplImage *temp;
if(queue.try_pop(temp)){
cvShowImage("tacka", temp);
cvWaitKey(1);
}
tbb::tick_count t0 = tbb::tick_count::now();
tbb::this_tbb_thread::sleep(tbb::tick_count::interval_t(.04));
tbb::tick_count t1 = tbb::tick_count::now();
printf("time%g \n",(t1-t0).seconds());
}
}
the time period is not .04 second. it is more than that. i have to showimage every .04 second. what is the best way to do it.
0 Kudos
RafSchietekat
Valued Contributor III
540 Views
You didn't specify how much more, and if you want to show 25 images per second you should sleep less than 40 ms.

Try to find a timer utility, or write your code to tune itself. TBB uses Sleep(), which has only millisecond granularity, and I don't know how well the scheduler supports it: perhaps this could be rewritten to busy-wait the last few milliseconds for better accuracy?
0 Kudos
hitach__0
Beginner
540 Views
My code is working, but using sleep() method we cant sure that it will run every 40ms. because by using sleep() we are saying that, that thread will runat least40ms.
But i have to force that method to run on every 40ms. can weinterruptthat method on every 40ms?
I didnt understand why your mentioning timer utility, sleep() can support ms value (i think it support upto for decimal point).
are you saying that instead of sleep(), we have to use timer?
Thanks
0 Kudos
RafSchietekat
Valued Contributor III
540 Views
"are you saying that instead of sleep(), we have to use timer?"
If you want to drive nails, why not use a hammer...

(Added) That means: yes. :-)
0 Kudos
Reply