
Originally Posted by
Abdelhadi
[B]1 - about
QThreadPool, do i have to create 10 different instances for ChildA/ChildB ?!, can you give me a good practice for this method

More or less.
The idea of a thread pool is that you have tasks. You create an instance of classes with a certain interface, each instance handling a single task. Then you enqueue these task objects on a thread pool.
The pool then has each of its thread process one task and pick a new one when they are done (or make them wait until new tasks are available).
You code snippet looked a lot like that approach.
class ChildATask : public ChildA, public QRunnable
{
public:
ChildATask
(const QUrl &url
) : ChildA
(), m_url
(url
) {}
void run() {
DoSomething(m_url);
}
private:
};
class ChildATask : public ChildA, public QRunnable
{
public:
ChildATask(const QUrl &url) : ChildA(), m_url(url) {}
void run() {
DoSomething(m_url);
}
private:
QUrl m_url;
};
To copy to clipboard, switch view to plain text mode
Or comibine that into ChildA itself.
while (!queue.isEmpty()){
QUrl url
= queue.
dequeue();
if(url.scheme() == ...){
Parent * p = new ChildATask;
threadPool.start(p);
}
else if (url.scheme() == ...){
Parent * p = new ChildB;
threadPool.start(p);
}
}
while (!queue.isEmpty()){
QUrl url = queue.dequeue();
if(url.scheme() == ...){
Parent * p = new ChildATask;
threadPool.start(p);
}
else if (url.scheme() == ...){
Parent * p = new ChildB;
threadPool.start(p);
}
}
To copy to clipboard, switch view to plain text mode

Originally Posted by
Abdelhadi
2 - about QSemaphore, how i manage my QQueue with it ?
The semaphore would be used to limit the amount of parallelism, not the access to the queue.
Lets assume that each of your Child classes uses a separate thread and each thread would call release() on the semaphore before it exits.
So your work distribution loop would do something like this:
while (!queue.isEmpty()){
semaphore.acquire();
QUrl url
= queue.
dequeue();
if(url.scheme() == ...){
Parent * p = new ChildA;
p.DoSomething(url);
}
else if (url.scheme() == ...){
Parent * p = new ChildB;
p.DoSomething(url);
}
}
while (!queue.isEmpty()){
semaphore.acquire();
QUrl url = queue.dequeue();
if(url.scheme() == ...){
Parent * p = new ChildA;
p.DoSomething(url);
}
else if (url.scheme() == ...){
Parent * p = new ChildB;
p.DoSomething(url);
}
}
To copy to clipboard, switch view to plain text mode
If the semaphore has been initialized with 10, then the loop can acquire() it then times before any thread needs to be done. After that it will wait in acquire() until at least one of the 10 already running threads is done.
Cheers,
_
Bookmarks