You are not logged in.

1

Wednesday, February 27th 2008, 2:44pm

Qt Newbie -- QThread Question

Hi Qt community-

I'm brand new to Qt. I've been doing a lot of research into QThread and the Signals & Slots mechanism, and am trying to put together the last piece of the following puzzle:

Let's say object A needs some work done. Object A queues its request into a standard semaphore'd/mutex'd request queue.

There is a thread pool of objects (QThread subclasses) hanging out, continually checking the queue for requests to process. Object B steps up to the plate.

My question is: what is the best method for object B to signal object A when it's done processing object A's request? We don't want a "permanent" connect() because it may not be object B that handles A's request -- it may be C, D, or E.

Since signals are inherently protected, object B cannot just call A->emit(workDone()), right? So that leads to object A having a public function that simply calls this->emit(workDone()). But that seems like a bit of a hack.

I hope my question is clear. Any help would be greatly appreciated.

Thanks.

--scope

This post has been edited 1 times, last edit by "bfchooch" (Feb 27th 2008, 2:46pm)


2

Wednesday, February 27th 2008, 6:53pm

RE: Qt Newbie -- QThread Question

Quoted

Originally posted by bfchooch
...
I hope my question is clear.

not exactly!!!
Nicolas

DrDonut

Beginner

Posts: 35

Location: The Netherlands

Occupation: student

  • Send private message

3

Monday, March 3rd 2008, 10:35am

Wouldn't it be much easier not to create this B, C, D, and E in the beginning of your program, but to create a thread when a request comes. The the thread can process this request and after it's done terminate itself.

I think this is much easier and takes less resources, but i'm a QT newbie too so I might be wrong.

Good Luck

yrenster

Beginner

Posts: 6

Location: Atlanta, USA

Occupation: Softwate Engineer

  • Send private message

4

Monday, March 3rd 2008, 8:49pm

RE: Qt Newbie -- QThread Question

There are several things you need to include in your pool:
a) you need a job id for each of your request.
b) design a signal in the pool threads as, workDone(id).
c) design a slot in thread A to catch the signa. You will need to add the event loop in thread A to do that.

This way, when A catches the sugnal, it will know which job is done.

5

Monday, March 3rd 2008, 9:16pm

RE: Qt Newbie -- QThread Question

yrenster-

Thanks for your reply. I think I understand your motivations for such a design, but why is that solution better than:

class A {
public:
void SignalWorkDone( void ) { emit WorkDone(); }
signals:
void WorkDone( void );
}


1) Thread B finishes object A's work
2) Thread B calls A->SignalWorkDone()
3) Thread A's slot to handle the signal is executed

With the design you proposed, object A would get a signal each time _any_ job is completed, not just those submitted by object A. Or I could be misunderstanding something.

Thanks again for your help.

--scope

yrenster

Beginner

Posts: 6

Location: Atlanta, USA

Occupation: Softwate Engineer

  • Send private message

6

Saturday, March 15th 2008, 3:56am

RE: Qt Newbie -- QThread Question

I see your point. Your approch is still very "involved" by calling A->SignalWorkDone(). Try the following:

class A {
public:
slot SignalWorkDone( void );
};

class B {

signal WorkDone();
};

in B's constructor:
connect(this, SIGNAL(WorkDone()), A, SLOT(SignalWorkDone()));

When B finishes A's work, emit WorkDone(); That way, A will catch the signal. All I'm saying is, it is a lot cleaner than B calling A's method.