tbb and gui development

tbb and gui development

Is it possible to use tbb in gui application?

I would like to do the following.
For example I have some GUI application.
There is a button. If user press the button application runs some activity in bacground(in another thread).
After that the button is going to disable. And user can continue work with application(of course he can't click the button again, because it is disabled).

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();
}

The first quastion is situated in the comment

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?

Thanks.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

It seems you try to usea task as you would use a thread. You better use a thread, then :) (e.g. a tbb_thread in case you find it suitable). And if that thread had a lot of work to do, it could use TBB tasks or algorithms on its own.
For communication between the GUI thread and the worker thread, you could use events or messages or whatever stuff is available for that purpose in your programming platform.

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.

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;
};

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?
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;
};

Best Reply

Quoting Denis Bolshakov
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.

Leave a Comment

Please sign in to add a comment. Not a member? Join today