Create many objects using threads

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 5 10:25:29 PDT 2014


On 05/05/2014 10:14 AM, Caslav Sabani wrote:

 > I want to have one array where I will store like 100000  objects.
 >
 > But I want to use 4 threads where each thread will create 25000 objects
 > and store them in array above mentioned.

1) If it has to be a single array, meaning that all of the objects are 
in consecutive memory, you can create the array and give four slices of 
it to the four tasks.

To do that, you can either create a proper D array filled with objects 
with .init values; or you can allocate any type of memory and create 
objects in place there.

2) If it doesn't have to a single array, you can have the four tasks 
create four separate arrays. You can then use them as a single range by 
std.range.chain. This option allows you to have a single array as well.

I would like to give examples of those methods later. Gotta go now... :)

 > And all 4 threads should be working in parallel because I have 4 core
 > processor for example. I do not care in which order objects are 
created nor
 > objects should be aware of one another. I just need them stored in array.
 > Can threading help in creating many objects at once?  Note that I am
 > beginner at working with threads so any help is welcome :) Thanks

I recommend looking at std.parallelism first and then std.concurrency. 
Here are two chapters that may be helpful:

   http://ddili.org/ders/d.en/parallelism.html

   http://ddili.org/ders/d.en/concurrency.html

Ali



More information about the Digitalmars-d-learn mailing list