Multithreaded file IO?

Jerry Quinn jlquinn at optonline.net
Fri Sep 23 20:01:17 PDT 2011


Hi folks,

I wasn't sure whether this should go here or in the D devel list...

I'm trying to port a program where threads read from a file, process the data, then write the output data.  The program is cpu-bound.  In C++ I can do something like this:

class QueueIn {
  ifstream in;
  mutex m;

  string get() {
    string s;
    m.lock();
    getline(in, s);
    m.unlock();
   return s;
  }
};

class QueueOut {
  ofstream out;
  mutex m;
  void put(string s) {
    m.lock();
    out.write(s);
    m.unlock();
  }
};


In D, I'm so far having trouble figuring out the right idiom to do what I want.  I looked at std.parallel, but it seems a bit tricky to make my stuff work in this setting.  A std.stdio File cannot be part of shared class.

How would you do this with the latest D2?

Thanks
Jerry



More information about the Digitalmars-d-learn mailing list