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

tbb and gui development

Denis_Bolshakov
Beginner
665 Views
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).

[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]

The first quastion is situated in the comment
[cpp]What I should return??? Is it parent task, child task, null pointer.
[/cpp]
And the most important quastion. How to call onActivityFinished when task is finished the job?

Thanks.
0 Kudos
1 Solution
Alexey-Kukanov
Employee
665 Views
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.

View solution in original post

0 Kudos
4 Replies
Alexey-Kukanov
Employee
665 Views
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.

0 Kudos
Denis_Bolshakov
Beginner
665 Views

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.

0 Kudos
Denis_Bolshakov
Beginner
665 Views

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?

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;
};
0 Kudos
Alexey-Kukanov
Employee
666 Views
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.

0 Kudos
Reply