Error running concurrent process and storing results in array

drug drug2004 at bk.ru
Wed May 6 05:44:47 UTC 2020


06.05.2020 07:25, data pulverizer пишет:
> On Wednesday, 6 May 2020 at 03:56:04 UTC, Ali Çehreli wrote:
>> On 5/5/20 8:41 PM, data pulverizer wrote:> On Wednesday, 6 May 2020 at 
>> 03:33:12 UTC, Mathias LANG wrote:
>> >> On Wednesday, 6 May 2020 at 03:25:41 UTC, data pulverizer
>> wrote:
>>
>> > Is there something I need to do to wait for each thread to
>> finish
>> > computation?
>>
>> thread_joinAll(). I have an example here:
>>
>> http://ddili.org/ders/d.en/concurrency.html#ix_concurrency.thread_joinAll
> 
> This worked nicely thank you very much
> 
>> ... I want to point out that there is also std.parallelism, which may 
>> be better suited in many cases.
> 
> I actually started off using std.parallelism and it worked well but the 
> CPU usage on all the threads was less than half on my system monitor 
> meaning there is more performance to be wrung out of my computer, which 
> is why I am now looking into spawn. When you suggested using 
> thread_joinAll() I saw that is in `core.thread.osthread` module. It 
> might be shaving the yak this point but I have tried using `Thread` 
> instead of `spawn`:
> 
> ```
> void process(double x, double y, long i, shared(double[]) z)
> {
>    z[i] = x*y;
> }
> 
> void main()
> {
>    import core.thread.osthread;
>    import std.stdio: writeln;
> 
>    long n = 100;
>    shared(double[]) z = new double[n];
>    for(long i = 0; i < n; ++i)
>    {
>      auto proc = (){
>        process(cast(double)(i), cast(double)(i + 1), i, z);
>        return;
>      };

proc is already a delegate, so &proc is a pointer to the delegate, just 
pass a `proc` itself

>      new Thread(&proc).start();
>    }
>    thread_joinAll();
>    writeln("z: ", z);
> }
> ```
> and I am getting the following error:
> 
> ```
> onlineapp.d(20): Error: none of the overloads of this are callable using 
> argument types (void delegate() @system*), candidates are:
> /dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(646):        
> core.thread.osthread.Thread.this(void function() fn, ulong sz = 0LU)
> /dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(671):        
> core.thread.osthread.Thread.this(void delegate() dg, ulong sz = 0LU)
> /dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(1540):        
> core.thread.osthread.Thread.this(ulong sz = 0LU)
> ```
> 
> 
> 



More information about the Digitalmars-d-learn mailing list