PDA

View Full Version : ThreadPool ?



Fastman
14th August 2007, 08:13
Who can that will prompt as to organize ThreadPool? I shall be glad to examples. It is necessary to set the maximal size of a pool, to operate streams (start, end of a stream)

wysota
14th August 2007, 10:51
I don't entirely understand what you want, but if you want to have a thread pool, you'll need a wait condition that will start or stop threads from within a pool and an array of thread objects. If you want something more advanced, a controller spawning and destroying threads should be implemented as well. Trying QtConcurrent (available at labs.trolltech.com) might be a good idea as well.

Fastman
14th August 2007, 11:05
I need to store the list of the started processes (or them ID). During work I should have an opportunity at any moment to finish process.

wysota
14th August 2007, 12:28
Processes or threads? Are you using QProcess or QThread to spawn them? Both of these classes allow you to terminate the entity they control.

Fastman
14th August 2007, 14:58
Processes or threads? Are you using QProcess or QThread to spawn them? Both of these classes allow you to terminate the entity they control.

QThread !
I think to use QList for this purpose:
create :


QList<CWorkThread*> threadList;

if( threadList.size() < 4)
{
CWorkThread *thread = new CWorkThread(this);
threadList.append(thread);
thread->run();
}

and for terminate:


for(int i = 0; i < threadList.size(); i++)
{
CWorkThread *nb = threadList.at(i);
nb->terminate();
nb->wait();
threadList.erase(threadList.begin()+i);
i--;
}

Whether there will be it the correct decision???

wysota
14th August 2007, 19:43
First of all you should never use terminate() unless you know what you are doing. Second of all I don't see any "pool" here, just a bunch of threads. I think you should just let all threads do their job and connect to each thread's finished() signal and delete the thread when it's done doing whatever it was doing.

Fastman
15th August 2007, 07:56
I shall think above that that you have told. But in me can be caused 4-5 threads for copying files in the size 10-80 Gb, and there should be an opportunity to stop copying. There are other ways of the decision?

marcel
15th August 2007, 08:00
Yes, you could tell the thread to stop.
The most common way is to add a volatile boolean flag in the thread and a setter.
When you set this flag to true(from outside the thread) it means the thread should stop. You have to test for this flag in the thread in all extensive operations.
For example, if you copy the files in a loop, then you should test that flag and break the loop when it is false.

Regards