object splitting in multiple threads
Denis Koroskin
2korden at gmail.com
Fri Jan 9 20:49:15 PST 2009
On Sat, 10 Jan 2009 07:32:43 +0300, yes <yes at no.com> wrote:
>>
>> What does this code do? It looks like a ThreadPool use case but I might
>> be wrong.
>>
>
> I tried to recursively make an object distribute its calculations.
> The calculations should take at least a minute.
Hm... I guess I understand now, you need a task parallelization, right?
In order to do this, you usually separate the task into several independent sub-tasks, put into a queue and wait until it is finished.
For example, we have a list of files to download from remote server. For each file, we create a new connection and retrieve it independently (and, possibly, out of order):
import tango.core.ThreadPool;
void execute()
{
auto pool = new ThreadPool!(string)(10); // create a thread pool with 10 threads
string[] fileList = loadFileList();
foreach (string filePath; fileList) {
pool.append(&download, filePath); // append the task
}
pool.finish(); // wait until all the tasks complete
}
void download(string fileName)
{
// ...
}
In this example, I create a lot of threads (more than hardware runs concurrently) because the CPU is not a bottleneck.
You may experience different results in different CPU workaload, though.
Hope that helps.
More information about the Digitalmars-d-learn
mailing list