www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Threading errors.

reply dcoder <dcoder devnull.dev> writes:
Hello.

I am working through Alexandrescu's "The D Programming Language".  He has an
example in his concurrency chapter which I can't compile.  It looks to be
missing a few import packages, but getting over that, I still can't get the
example to work:

import std.concurrency, std.stdio;
import std.contracts, std.typecons;  /* I added this */

void main() {
  auto low = 0, high = 25;
  auto tid = spawn(&writer);

  foreach( i; low .. high) {
    writeln("Main thread: ", i);
    tid.send(thisTid, i);
    enforce( receiveOnly!Tid() == tid);
  }

  return;
}

void writer() {
  for( ;; ) {
    auto msg = receiveOnly!(Tid, int)();
    writeln( "Secondary thread: ", msg[1]);
    msg[0].send(thisTid);
  }

  return;
}


I get the following compiler error:

$ dmd Thread2.d

Thread2.d(10): Error: template std.typecons.Tuple!(Tid).Tuple.opEquals(T) if
(is(typeof(T.field))) does not match any function template declaration
Thread2.d(10): Error: template std.typecons.Tuple!(Tid).Tuple.opEquals(T) if
(is(typeof(T.field))) cannot deduce template function from argument types
!()(Tid)


$ dmd --help
Digital Mars D Compiler v2.042
Copyright (c) 1999-2010 by Digital Mars written by Walter Bright
Documentation: http://www.digitalmars.com/d/2.0/index.html


So, what am I doing wrong here?

thanks.
Jul 26 2010
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 26.07.2010 19:45, dcoder wrote:
 Hello.

    
[snip]
 $ dmd --help
 Digital Mars D Compiler v2.042
 Copyright (c) 1999-2010 by Digital Mars written by Walter Bright
 Documentation: http://www.digitalmars.com/d/2.0/index.html


    
I suggest updating compiler - it's should be 2.047 now. The outdated distribution could also feature some bugs in std.concurency.
 So, what am I doing wrong here?

 thanks.

    
-- Dmitry Olshansky
Jul 26 2010
parent reply Rory Mcguire <rjmcguire gm_no_ail.com> writes:
Dmitry Olshansky wrote:

 On 26.07.2010 19:45, dcoder wrote:
 Hello.

    
[snip]
 $ dmd --help
 Digital Mars D Compiler v2.042
 Copyright (c) 1999-2010 by Digital Mars written by Walter Bright
 Documentation: http://www.digitalmars.com/d/2.0/index.html


    
I suggest updating compiler - it's should be 2.047 now. The outdated distribution could also feature some bugs in std.concurency.
 So, what am I doing wrong here?

 thanks.

    
Doesn't work for 2.047 either. dthreadpass.d(19): Error: no [] operator overload for type Tuple!(Tid,int) dthreadpass.d(20): Error: no [] operator overload for type Tuple!(Tid,int)
Jul 26 2010
parent reply dcoder <dcoder devnull.dev> writes:
== Quote from Rory Mcguire (rjmcguire gm_no_ail.com)'s article
 Dmitry Olshansky wrote:
 On 26.07.2010 19:45, dcoder wrote:
 $ dmd --help
 Digital Mars D Compiler v2.042
 Copyright (c) 1999-2010 by Digital Mars written by Walter Bright
 Documentation: http://www.digitalmars.com/d/2.0/index.html
I suggest updating compiler - it's should be 2.047 now. The outdated distribution could also feature some bugs in std.concurency.
Doesn't work for 2.047 either. dthreadpass.d(19): Error: no [] operator overload for type Tuple!(Tid,int) dthreadpass.d(20): Error: no [] operator overload for type Tuple!(Tid,int)
Yep, I get that same error after upgrading my compiler. $ dmd --help Digital Mars D Compiler v2.047 Copyright (c) 1999-2010 by Digital Mars written by Walter Bright Documentation: http://www.digitalmars.com/d/2.0/index.html thanks.
Jul 26 2010
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Mon, Jul 26, 2010 at 19:11, dcoder <dcoder devnull.dev> wrote:

 == Quote from Rory Mcguire (rjmcguire gm_no_ail.com)'s article
 Dmitry Olshansky wrote:
std.typecons.Tuple fields cannot be indexed like arrays, Andrei made a mistake. To access field #n, use ._n or .field[n]. There is no difference between the two. void writer() { for( ;; ) { auto msg = receiveOnly!(Tid, int)(); // msg is a Tuple!(Tid, int), msg._0 is a Tid, msg._1 is an int. writeln( "Secondary thread: ", msg._1); msg._0.send(thisTid); } } Also, in my case, the return; in writer must be commented out, or DMD complains it cannot be reached. Philippe
Jul 26 2010
parent reply Rory Mcguire <rjmcguire gm_no_ail.com> writes:
Philippe Sigaud wrote:

 On Mon, Jul 26, 2010 at 19:11, dcoder <dcoder devnull.dev> wrote:
 
 == Quote from Rory Mcguire (rjmcguire gm_no_ail.com)'s article
 Dmitry Olshansky wrote:
std.typecons.Tuple fields cannot be indexed like arrays, Andrei made a mistake. To access field #n, use ._n or .field[n]. There is no difference between the two. void writer() { for( ;; ) { auto msg = receiveOnly!(Tid, int)(); // msg is a Tuple!(Tid, int), msg._0 is a Tid, msg._1 is an int. writeln( "Secondary thread: ", msg._1); msg._0.send(thisTid); } } Also, in my case, the return; in writer must be commented out, or DMD complains it cannot be reached. Philippe
Interesting, I didn't have to comment out return; using dmd 2.047 on linux
Jul 27 2010
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, Jul 27, 2010 at 11:25, Rory Mcguire <rjmcguire gm_no_ail.com> wrote:

 Also, in my case, the return; in writer must be commented out, or DMD
 complains it cannot be reached.
 Interesting, I didn't have to comment out return; using dmd 2.047 on linux
I think I have -w (warnings treated as errors?) always checked. That's my Code::Blocks default configuration for DMD. I was on Windows for this test. I never cheked if there was any difference between OSes for this :-)
Jul 27 2010
parent reply Rory Mcguire <rjmcguire gm_no_ail.com> writes:
Philippe Sigaud wrote:

 On Tue, Jul 27, 2010 at 11:25, Rory Mcguire <rjmcguire gm_no_ail.com>
 wrote:
 
 Also, in my case, the return; in writer must be commented out, or DMD
 complains it cannot be reached.
 Interesting, I didn't have to comment out return; using dmd 2.047 on
 linux
I think I have -w (warnings treated as errors?) always checked. That's my Code::Blocks default configuration for DMD. I was on Windows for this test. I never cheked if there was any difference between OSes for this :-)
:) thanks Philippe
Jul 27 2010
parent dcoder <dcoder devenull.com> writes:
== Quote from Rory Mcguire (rjmcguire gm_no_ail.com)'s article
 Philippe Sigaud wrote:
 Also, in my case, the return; in writer must be commented out, or DMD
 complains it cannot be reached.
 Interesting, I didn't have to comment out return; using dmd 2.047 on
 linux
I think I have -w (warnings treated as errors?) always checked. That's my Code::Blocks default configuration for DMD. I was on Windows for this test. I never cheked if there was any difference between OSes for this :-)
:) thanks Philippe
Actually, after making the changes that you suggest, Philippe my program works. Thanks. However, I did not have to comment out the return statement. But when I did compile using the -w flag, I do get the compiler error that you describe. Anyways, thanks for the coding suggestion.
Jul 27 2010
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
The next release, 2.048, should bring things in line with TDPL.  I had meant to
do this for 2.047, but was too busy with other work to finish in time.
Jul 27 2010
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, Jul 27, 2010 at 19:03, Sean Kelly <sean invisibleduck.org> wrote:

 The next release, 2.048, should bring things in line with TDPL.  I had
 meant to do this for 2.047, but was too busy with other work to finish in
 time.
Do you mean, indexing tuples directly by a CT value as in msg[1]? Because that was the only error here, I think.
Jul 27 2010
parent Andrej Mitrovic <andrej.mitrovich test.com> writes:
So what's the word on this? Will we have simple indexing of tuples via T[] or
do we still need T.field[] and T._1 ? The situations is the same in 2.048 as it
was in 2.047.

There are more examples of tuples being used via T[] in some Phobos code
examples (which don't compile and I've reported that).

Otherwise if it stays the way it is I'll send Andrei a message to add the
corrections to the errata.

Philippe Sigaud Wrote:

 On Tue, Jul 27, 2010 at 19:03, Sean Kelly <sean invisibleduck.org> wrote:
 
 The next release, 2.048, should bring things in line with TDPL.  I had
 meant to do this for 2.047, but was too busy with other work to finish in
 time.
Do you mean, indexing tuples directly by a CT value as in msg[1]? Because that was the only error here, I think. <div class="gmail_quote">On Tue, Jul 27, 2010 at 19:03, Sean Kelly <span dir="ltr">&lt;<a href="mailto:sean invisibleduck.org">sean invisibleduc .org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> The next release, 2.048, should bring things in line with TDPL.  I had meant to do this for 2.047, but was too busy with other work to finish in time.<br> </blockquote></div><br><div>Do you mean, indexing tuples directly by a CT value as in msg[1]? Because that was the only error here, I think.</div><div><br></div>
Sep 03 2010