PDA

View Full Version : Event queue size



naroin
29th November 2010, 08:17
hi,

is it possible to see the event queue for a given object?

I want to be able to know if I send too many events, and then drop some.

wysota
29th November 2010, 08:49
Objects don't have their own event queues. There is one event queue per thread for all objects living in that thread. And as far as I know there is no way of removing events from the queue without processing them.

naroin
29th November 2010, 09:46
Thanks for your answer.

What i want is the event queue size for a given THREAD, you're right :)

But I don't want to remove events, but just not send them. To know if i must not send an evnet, i need to know the event queue size for the thread i want to send it.

is is possible?

wysota
29th November 2010, 10:04
What i want is the event queue size for a given THREAD, you're right :)

But I don't want to remove events, but just not send them. To know if i must not send an evnet, i need to know the event queue size for the thread i want to send it.
Hmm... so, let's say the event loop has 10 events pending, does it give you any meaningful information? You don't know what events are they...

Please state what you need this functionality for, there is probably an easier way to do what you want.

naroin
29th November 2010, 10:13
I've got something like a server that receive lots of "items".

For each item, i have to make different actions, for instance saving it into database, or do some calculations on it. So i have a thread "db" and a thread "calculate".
If the db is slow, there's a risk that i send too many items to it while they are not treated in time, and then the application will crash.
So i want to drop some items : do not send it to the thread that is too charged.

wysota
29th November 2010, 12:21
And how is the size of event loop going to help you with this? Do the following instead:

QMutex mutex;
QWaitCondition cond;

struct Job {
Something data;
QDateTime timeToLive;
};

QQueue<Job> queue;

class DatabaseHandler : public QThread {
public:
void run() {
... = QSqlDatabase::addDatabase(...);
// ...
mutex.lock();
while(cond.wait(&mutex)) {
mutex.lock();
while(!queue.isEmpty()){
Job job = queue.dequeue();
mutex.unlock();
if(job.timeToLive >= QDateTime::currentDateTime()) {
processJob(job);
}
mutex.lock();
}
}
// ...
QSqlDatabase::removeDatabase(...);
}
};

Write the other end of the communication yourself. Oh, and provide a stop condition for the thread.

naroin
29th November 2010, 12:52
I wanted to do that with signals & slots, but yes, your solution is good.

Thanks a lot

wysota
29th November 2010, 13:26
Signals and slots are not an ultimate solution to everything. There is no point in using signals and slots if the emitting object apriori expects a particular object and particular slot of that object to respond to this signal. You can instead trigger the functionality directly since the objects are tightly coupled anyway.