www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the best practice of organization multi-threading ?

reply "Suliman" <evermind live.ru> writes:
I have App that read config sections. If the section is true I 
want to run it in new thread.

	if (parseconfig.obsmpplots_load == "true")
	{
		auto obsmpplots = new ObsmpPlots(db);
		auto theTask = task(&obsmpplots.getPlots);
		theTask.executeInNewThread();

	}

	if(parseconfig.nadisa_load == "true")
	{
		auto nadisa = new Nadisa(db);
		auto theTask = task(&nadisa.getPlots);
		theTask.executeInNewThread();
	}

It's seems work, but I do not sure that I doing it's right.
Mar 15 2015
next sibling parent "Suliman" <evermind live.ru> writes:
UP
Mar 16 2015
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/15/2015 01:53 PM, Suliman wrote:
 I have App that read config sections. If the section is true I want to
 run it in new thread.

      if (parseconfig.obsmpplots_load == "true")
      {
          auto obsmpplots = new ObsmpPlots(db);
          auto theTask = task(&obsmpplots.getPlots);
          theTask.executeInNewThread();

      }

      if(parseconfig.nadisa_load == "true")
      {
          auto nadisa = new Nadisa(db);
          auto theTask = task(&nadisa.getPlots);
          theTask.executeInNewThread();
      }

 It's seems work, but I do not sure that I doing it's right.
I don't see any problem. The threads start, do their jobs, and terminate. If the threads need to produce results that they need to pass to their owner (as messages), then std.concurrency is another option. Ali
Mar 16 2015
parent reply "Suliman" <evermind live.ru> writes:
Ali, again big thanks for your book, without it I was not able 
how to work with D.

But for me now absolutely clear what difference between:
auto theTask = task(&someFunction);
and:
auto theTask = task!anOperation();
Mar 16 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/16/2015 11:28 AM, Suliman wrote:

 difference between:
 auto theTask = task(&someFunction);
 and:
 auto theTask = task!anOperation();
tl;dr - Prefer task!anOperation() unless you can't. :) task() is flexible enough to take what to execute either as a function (or delegate) pointer (task(&someFunction)) or as a template parameter (task!anOperation). The template method is more flexible because it can take anything that can be executed. For that reason, I would prefer task!anOperation. However, because it is a template parameter, the Task template instances that are generated would not have the same type (even though two functions have the same signature). This would e.g. prevent you from putting two Task instances in an array: import std.parallelism; void foo() {} void bar() {} void main() { auto tasks = [ task!foo(), task!bar() ]; // COMPILATION ERROR } Error: incompatible types for ((task()) : (task())): 'Task!(foo)*' and 'Task!(bar)*' So, you would have to give the function as a pointer: auto tasks = [ task(&foo), task(&bar) ]; However, this overload actually do the same thing behind the scenes and calls task!run(myFunc) behind the scenes (run() is a function template defined in parallelism.d). Ali
Mar 16 2015
parent reply "Suliman" <evermind live.ru> writes:
Ali, please add text above to your book!
Mar 16 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/16/2015 12:04 PM, Suliman wrote:
 Ali, please add text above to your book!
Will do. Please email me your full name at acehreli yahoo.com so that I can add it to the Acknowledgments section. Thank you, Ali
Mar 16 2015