multithreading & sqlite

Lars Johansson lasse at 11dim.se
Tue Jan 27 16:38:08 UTC 2026


I have created a program using multithreading and Sqlite3.
This is a prestudy for map&reduce.

I found some parts of the program very hard to digest especially 
the 'as!' crap.
Sqlite was not simple (for me) to set up and use. It took some 
time to understand I must make a physical copy of the result (the 
.idup).
It was hard to make this work.

I would appreciate, if anyone can comment on things I can do 
better:
(On your own, it not easy to know what can be done better)

import std.parallelism;
import std.stdio;
import core.thread;
import std.array;
import d2sqlite3;

     void main() {
         writeln("starting");
         auto myresults = getData("mydata.db");
         auto pool = new TaskPool(2); // Create pool with only 2 
worker threads
         foreach (i,row; pool.parallel(myresults)) {
             writeln(row);
            auto ID = row[0];
            auto Name = row[1];
            auto Age= row[2];
     		writeln("Starting task ",i,  " ",ID, " ",Name, " ", Age);
     		Thread.sleep(1.seconds);  // Simulate work
             writeln("finish task ",i);
         }
         pool.finish();
     }
     string[][] getData(string DB){
         string[][] results;
         writeln("getData starts");
         auto db = Database(DB);
         foreach (row; db.execute("SELECT * FROM users")) {
                 writeln("ID: ", row[0].as!int);   //WTF is this 
as! crap
                 writeln("Name: ", row[1].as!string);
                 writeln("Age: ", row[2].as!int);
             results ~= [
                     row[0].as!string.idup,  // .idup makes 
immutable copy
                     row[1].as!string.idup,  // why do i need this?
                     row[2].as!string.idup   // and why do I have 
to convert to string?
             ];}
             writeln("getData done");
         return results;
     }


More information about the Digitalmars-d-learn mailing list