On Saturday, 19 November 2016 at 19:04:12 UTC, Nicolas Gurrola wrote:
On Saturday, 19 November 2016 at 17:29:30 UTC, Konstantin Kutsevalov wrote:
I need to receiving data in main thread and send its to other thread for processing. There is a simple (but wrong) code for example.
What need I to change to make it correct?

```
import std.stdio, std.string, std.array, core.thread, std.datetime, std.conv;

int main() {
        Pumpurum pp = new Pumpurum();
        Thread ppt = new Thread(&pp.processingData);
        ppt.start();
        while(pp.waitData())
        {
                // some operations may be
        }
        writeln("finished main");
        return 0;
}

class Pumpurum
{
        private string[string] Data;
        
        private string[string] Result;
        
        private bool _quit = false;
        
        private int _counter = 0;
        
        bool waitData()
        {
                // waits for new data and adds to Data
                string key;
string line = readln(); // just for example, in real its will be data from several socket connections
                if (line !is null) {
                        if (line == "quit\n") {
                                this._quit = true;
                                return false;
                        }
key = Clock.currTime().toString() ~ " " ~ to!string(this._counter);
                        this.Data[key] = line; // adds new data to "Data"
                }
                return true;
        }
        
        void processingData()
        {
                string key, value;
                while(!this._quit) {
                        if (this.Data.length > 0) {
// todo checks the "Data" for some data, processing its and saves a result to "Result"
                                foreach (key, value; this.Data) {
                                        writeln(value); // some processing :)
                                        this.Result[key] = value;
                                        this.Data.remove(key);
                                }
                        }
                }
                writeln("finished processing");
        }
        
}
```

Any advice may help. Thank you.

I'd recommend using std.concurrency or std.parallelism instead of core.thread, as they're higher level APIs. You probably want to check out std.parallelism first to see if it has what you need. Otherwise, the generic messaging API of std.concurrency should be easier to use than core.thread while still being very flexible.

Ok, thank you. But I cannot to find good example. May be you know some good article about it?

Reply via email to