digitalmars.D.learn - How to correct share data between threads?
- Konstantin Kutsevalov (66/66) Nov 19 2016 I need to receiving data in main thread and send its to other
- Nicolas Gurrola (7/73) Nov 19 2016 I'd recommend using std.concurrency or std.parallelism instead of
- Konstantin Kutsevalov (4/83) Nov 20 2016 Ok, thank you. But I cannot to find good example. May be you know
- Nicolas Gurrola (12/14) Nov 20 2016 The Phobos documentation is a good place to start, as it has
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.
Nov 19 2016
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.
Nov 19 2016
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:Ok, thank you. But I cannot to find good example. May be you know some good article about it?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.
Nov 20 2016
On Sunday, 20 November 2016 at 17:50:38 UTC, Konstantin Kutsevalov wrote:Ok, thank you. But I cannot to find good example. May be you know some good article about it?The Phobos documentation is a good place to start, as it has examples as well as the documentation for all the APIs: https://dlang.org/phobos/std_concurrency.html and https://dlang.org/phobos/std_parallelism.html. For something more in depth, the chapter on concurrency from TDPL is available online, which covers std.concurrency: http://www.informit.com/articles/article.aspx?p=1609144. And Ali Çehreli's book is also available online, with chapters on std.parallelism (http://ddili.org/ders/d.en/parallelism.html) and std.concurrency (http://ddili.org/ders/d.en/concurrency.html).
Nov 20 2016