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.

    

 $ 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


    

distribution could also feature some bugs in std.concurency.
 So, what am I doing wrong here?

 thanks.

    

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

 On 26.07.2010 19:45, dcoder wrote:
 Hello.

    

 $ 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


    

distribution could also feature some bugs in std.concurency.
 So, what am I doing wrong here?

 thanks.

    


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

distribution could also feature some bugs in std.concurency.

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 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 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 :-)


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 next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001517479138185ffb048c502927
Content-Type: text/plain; charset=ISO-8859-1

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 --001517479138185ffb048c502927 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Mon, Jul 26, 2010 at 19:11, dcoder <span dir= =3D"ltr">&lt;dcoder devnull.dev&gt;</span> wrote:<br><blockquote class=3D"g= mail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(= 204, 204, 204); padding-left: 1ex;"> =3D=3D Quote from Rory Mcguire (<a href=3D"mailto:rjmcguire gm_no_ail.com">= rjmcguire gm_no_ail.com</a>)&#39;s article<br> <div class=3D"im">&gt; Dmitry Olshansky wrote:<br></div></blockquote><div><= br><br>std.typecons.Tuple fields cannot be indexed like arrays, Andrei made= a mistake. To access field=A0 #n, use ._n or .field[n]. There is no differ= ence between the two.<br> <br>void writer() {<br> =A0for( ;; ) {<br> =A0 =A0auto msg =3D receiveOnly!(Tid, int)(); // msg is a Tuple!(Tid, int)= , msg._0 is a Tid, msg._1 is an int.<br> =A0 =A0writeln( &quot;Secondary thread: &quot;, msg._1);<br> =A0 =A0msg._0.send(thisTid);<br> =A0} <br>}<br><br>Also, in my case, the return; in writer must be commente= d out, or DMD complains it cannot be reached.<br><br><br>Philippe<br><br></= div></div> --001517479138185ffb048c502927--
Jul 26 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0016e6d7ef8e42130a048c5da64d
Content-Type: text/plain; charset=ISO-8859-1

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 :-) --0016e6d7ef8e42130a048c5da64d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Tue, Jul 27, 2010 at 11:25, Rory Mcguire <spa= n dir=3D"ltr">&lt;<a href=3D"mailto:rjmcguire gm_no_ail.com">rjmcguire gm_n= o_ail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> <br><div class=3D"im">&gt; Also, in my case, the return; in writer must be = commented out, or DMD<br> &gt; complains it cannot be reached.<br> </div></blockquote><div>=A0</div><blockquote class=3D"gmail_quote" style=3D= "margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padd= ing-left: 1ex;"><div class=3D"im"><br> </div>Interesting, I didn&#39;t have to comment out return; using dmd 2.047= on linux<br> </blockquote></div><br>I think I have -w (warnings treated as errors?) alwa= ys checked. That&#39;s my Code::Blocks default configuration for DMD.<br>I = was on Windows for this test. I never cheked if there was any difference be= tween OSes for this :-)<br> <br><br> --0016e6d7ef8e42130a048c5da64d--
Jul 27 2010
prev sibling next 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 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
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0015173ff38e4edc33048c6c62bb
Content-Type: text/plain; charset=ISO-8859-1

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. --0015173ff38e4edc33048c6c62bb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Tue, Jul 27, 2010 at 19:03, Sean Kelly <span = dir=3D"ltr">&lt;<a href=3D"mailto:sean invisibleduck.org">sean invisibleduc= k.org</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"m= argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> The next release, 2.048, should bring things in line with TDPL. =A0I had me= ant to do this for 2.047, but was too busy with other work to finish in tim= e.<br> </blockquote></div><br><div>Do you mean, indexing tuples directly by a CT v= alue as in msg[1]? Because that was the only error here, I think.</div><div=
<br></div>

--0015173ff38e4edc33048c6c62bb--
Jul 27 2010