[cpp]class Activity : public tbb::task { public: virtual tbb::task* execute(); }; tbb::task* Activity::execute() { //here I do my work in backround. //What I should return??? } class Window { ..... void onButtonPress(); void onActivityFinished(); private Button* m_startNewTask; }; void Window::onButtonPress() { m_startNewTask->disable(); tbb::task activity = new Activity; tbb::task* thread = activity->execute(); } void Window::onActivityFinished() { m_startNewTask->enable(); } [/cpp]
[cpp]What I should return??? Is it parent task, child task, null pointer.And the most important quastion. How to call onActivityFinished when task is finished the job?
[/cpp]
True.
I have some concern about using an automatic variable for tbb_thread. This object is destroyed when the method is complete, so the newly created thread is kind of "lost" - as far as I remember, it will be left in "detached" state which means it will run to completion but you can not join() it (i.e. can not check whether it has completed or not). I recommend you to double-check with the reference manual, and think whether the behavior is acceptable.
Thanks Alexey,
It 's exactly what I want. I've just thought that there is some way to register callback or functor, that will be called when thread is finished.
Alexey, tbb_thread is the accaptable solution.
I've developed prototype using it and I am fine.
[bash]class Dialog : public gui::Window { public: Dialog() : m_startActivity(new gui::Button){} ~Dialog(){delete m_startActivity;} virtual bool enable() const{ return m_startActivity->enable(); } virtual void execute() { onButtonPressed(); } void onButtonPressed() { m_startActivity->enable(false); tbb::tbb_thread(engine::Acitivity(*m_startActivity)); } void onActivityFinished() { m_startActivity->enable(true); } private: gui::Button* m_startActivity; };[/bash]But I still have a qustion when code execution is reached tbb::tbb_thread(engine::Activity(*m_startActivity)) new thread will be created and engine::Activity::operato() will be called in new created thread, is it true?
True.
I have some concern about using an automatic variable for tbb_thread. This object is destroyed when the method is complete, so the newly created thread is kind of "lost" - as far as I remember, it will be left in "detached" state which means it will run to completion but you can not join() it (i.e. can not check whether it has completed or not). I recommend you to double-check with the reference manual, and think whether the behavior is acceptable.
For more complete information about compiler optimizations, see our Optimization Notice.