www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - independent or parallel process

reply "drpepper" <dp pepper.com> writes:
I want the function below to run independently -- like a unix
background process, without delaying subsequent code in main.  I
tried the following using std.parallelism:

void main() {
    function_a(int a, int b) {
     ...
    }

    auto new_task = task!function_a(11, 12);
    new_task.executeInNewThread(1);

    // more code ...
}


I get errors: core.thread.ThreadException ... unable to set
thread priority ...
Oct 17 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/17/2012 07:46 PM, drpepper wrote:
 I want the function below to run independently -- like a unix
 background process, without delaying subsequent code in main. I
 tried the following using std.parallelism:

 void main() {
 function_a(int a, int b) {
 ...
 }

 auto new_task = task!function_a(11, 12);
 new_task.executeInNewThread(1);

 // more code ...
 }


 I get errors: core.thread.ThreadException ... unable to set
 thread priority ...
I am not sure whether it is supposed to be, but the function must be a module-level function; so define it outside of main. When I did that, there was segmentation fault if I used thread priority. Workaround: 1) Take function out of main 2) Do not specify thread priority Created bug report: http://d.puremagic.com/issues/show_bug.cgi?id=8849 Ali
Oct 18 2012
parent reply "drpepper" <dp pepper.com> writes:
Thank you Ali,

Indeed, I originally had the function outside of main.

I have found another strange result with std.concurrency:

spawn(&function, array[0].length, array.length);
// generates compiling error: "cannot deduce template function
from arg types"

//Yet,
int var_one = array[0].length;
int var_two = array.length;
spawn(&function, var_one, var_two);
// compiles and runs



On Thursday, 18 October 2012 at 17:52:09 UTC, Ali Çehreli wrote:
 On 10/17/2012 07:46 PM, drpepper wrote:
 I want the function below to run independently -- like a unix
 background process, without delaying subsequent code in main. I
 tried the following using std.parallelism:

 void main() {
 function_a(int a, int b) {
 ...
 }

 auto new_task = task!function_a(11, 12);
 new_task.executeInNewThread(1);

 // more code ...
 }


 I get errors: core.thread.ThreadException ... unable to set
 thread priority ...
I am not sure whether it is supposed to be, but the function must be a module-level function; so define it outside of main. When I did that, there was segmentation fault if I used thread priority. Workaround: 1) Take function out of main 2) Do not specify thread priority Created bug report: http://d.puremagic.com/issues/show_bug.cgi?id=8849 Ali
Oct 18 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/18/2012 06:34 PM, drpepper wrote:
 Thank you Ali,

 Indeed, I originally had the function outside of main.

 I have found another strange result with std.concurrency:

 spawn(&function, array[0].length, array.length);
 // generates compiling error: "cannot deduce template function
 from arg types"

 //Yet,
 int var_one = array[0].length;
 int var_two = array.length;
 spawn(&function, var_one, var_two);
 // compiles and runs
Reduced code: void bar(T...)(void function(T) fn, T args) {} void foo(size_t i) {} void main() { bar(&foo, 42); // <-- compilation error } std.concurrency.spawn has the same signature as bar. The problem is with that signature: It requires that the arguments must match the parameters of the function. Changing the signature of spawn() to the signature of the following bar() should work: void bar(Func, T...)(Func fn, T args) if (__traits(compiles, fn(args))) {} void foo(size_t i) {} void main() { bar(&foo, int.init); // <-- now compiles bar(&foo, size_t.init); } I wonder whether others see any weakness in that signature. If not, it can be entered to bugzilla as an enhancement request. Ali
Oct 18 2012