Thread overview
What is the best practice of organization multi-threading ?
Mar 15, 2015
Suliman
Mar 16, 2015
Suliman
Mar 16, 2015
Ali Çehreli
Mar 16, 2015
Suliman
Mar 16, 2015
Ali Çehreli
Mar 16, 2015
Suliman
Mar 16, 2015
Ali Çehreli
March 15, 2015
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.
March 16, 2015
UP
March 16, 2015
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

March 16, 2015
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();
March 16, 2015
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

March 16, 2015
Ali, please add text above to your book!
March 16, 2015
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