www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.concurrency: ownerTid vs thisTid

reply miazo <miazo no.spam.please> writes:
Hi,

The following simple program:

import std.stdio, std.concurrency;

void f1() {
     writeln("owner: ", ownerTid);
     writeln("worker: ", thisTid);
}

void main() {
     writeln("owner: ", thisTid);
     writeln("worker: ", spawn(&f1));
}

Gives me the following result:

owner: Tid(18fd58)
worker: Tid(18fd58)
owner: Tid(24afe38)
worker: Tid(24afe38)

Is it correct? My expectation was that:
- thisTid called from main will be the same as ownerTid called 
from f1
- thisTid called from f1 will be the same as value returned by 
spawn()

Thank you for your help.
Feb 09 2016
next sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
It is OK, I guess the output is just mixed

On Tue, Feb 9, 2016 at 4:35 PM, miazo via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 Hi,

 The following simple program:

 import std.stdio, std.concurrency;

 void f1() {
     writeln("owner: ", ownerTid);
     writeln("worker: ", thisTid);
 }

 void main() {
     writeln("owner: ", thisTid);
     writeln("worker: ", spawn(&f1));
 }

 Gives me the following result:

 owner: Tid(18fd58)
 worker: Tid(18fd58)
 owner: Tid(24afe38)
 worker: Tid(24afe38)

 Is it correct? My expectation was that:
 - thisTid called from main will be the same as ownerTid called from f1
 - thisTid called from f1 will be the same as value returned by spawn()

 Thank you for your help.
Feb 09 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/09/2016 07:52 AM, Daniel Kozak via Digitalmars-d-learn wrote:
 It is OK, I guess the output is just mixed
Yes, that's an important point but it is not the reason. (Just tested.) I think this is just an issue with how Tid objects are printed. Otherwise, everything works as expected and although they print the same value, "worker1 != worker2" is true as well. Ali
Feb 09 2016
next sibling parent miazo <miazo no.spam.please> writes:
On Tuesday, 9 February 2016 at 15:55:34 UTC, Ali Çehreli wrote:
 On 02/09/2016 07:52 AM, Daniel Kozak via Digitalmars-d-learn 
 wrote:
 It is OK, I guess the output is just mixed
Yes, that's an important point but it is not the reason. (Just tested.) I think this is just an issue with how Tid objects are printed. Otherwise, everything works as expected and although they print the same value, "worker1 != worker2" is true as well. Ali
Well, I'm not concerned about the order of the output but the actual values of these ids. Shouldn't it be rather: owner: Tid(18fd58) worker: Tid(24afe38) owner: Tid(18fd58) worker: Tid(24afe38) or owner: Tid(24afe38) worker: Tid(18fd58) owner: Tid(24afe38) worker: Tid(18fd58) ?
Feb 09 2016
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
You are right:
import std.stdio, std.concurrency;
import std.concurrency : MessageBox;

struct MyTid
{
    MessageBox mbox;
}

static void f1() {
    auto tT =3D cast(MyTid)thisTid;
    auto oT =3D cast(MyTid)ownerTid;
    writeln("F1:worker: ", cast(void*)tT.mbox);
    writeln("F1:owner: ", cast(void*)oT.mbox);
}

void main() {
    auto tT =3D cast(MyTid)thisTid();
    auto sT =3D cast(MyTid)spawn(&f1);
    writeln("Main:worker: ", cast(void *)sT.mbox);
    writeln("Main:owner: ", cast(void *)tT.mbox);
}


On Tue, Feb 9, 2016 at 4:55 PM, Ali =C3=87ehreli <
digitalmars-d-learn puremagic.com> wrote:

 On 02/09/2016 07:52 AM, Daniel Kozak via Digitalmars-d-learn wrote:

 It is OK, I guess the output is just mixed
Yes, that's an important point but it is not the reason. (Just tested.) I think this is just an issue with how Tid objects are printed. Otherwise=
,
 everything works as expected and although they print the same value,
 "worker1 !=3D worker2" is true as well.

 Ali
Feb 09 2016
prev sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
https://github.com/D-Programming-Language/phobos/blob/v2.070.0/std/concurrency.d#L340

formattedWrite(sink, "Tid(%x)", &mbox);

should be:

formattedWrite(sink, "Tid(%x)", cast(void *)mbox);

On Tue, Feb 9, 2016 at 5:25 PM, Daniel Kozak <kozzi11 gmail.com> wrote:
 You are right:
 import std.stdio, std.concurrency;
 import std.concurrency : MessageBox;

 struct MyTid
 {
     MessageBox mbox;
 }

 static void f1() {
     auto tT = cast(MyTid)thisTid;
     auto oT = cast(MyTid)ownerTid;
     writeln("F1:worker: ", cast(void*)tT.mbox);
     writeln("F1:owner: ", cast(void*)oT.mbox);
 }

 void main() {
     auto tT = cast(MyTid)thisTid();
     auto sT = cast(MyTid)spawn(&f1);
     writeln("Main:worker: ", cast(void *)sT.mbox);
     writeln("Main:owner: ", cast(void *)tT.mbox);
 }


 On Tue, Feb 9, 2016 at 4:55 PM, Ali Çehreli
 <digitalmars-d-learn puremagic.com> wrote:
 On 02/09/2016 07:52 AM, Daniel Kozak via Digitalmars-d-learn wrote:
 It is OK, I guess the output is just mixed
Yes, that's an important point but it is not the reason. (Just tested.) I think this is just an issue with how Tid objects are printed. Otherwise, everything works as expected and although they print the same value, "worker1 != worker2" is true as well. Ali
Feb 09 2016
prev sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
OK it seems wierd

On Tue, Feb 9, 2016 at 4:52 PM, Daniel Kozak <kozzi11 gmail.com> wrote:

 It is OK, I guess the output is just mixed

 On Tue, Feb 9, 2016 at 4:35 PM, miazo via Digitalmars-d-learn <
 digitalmars-d-learn puremagic.com> wrote:

 Hi,

 The following simple program:

 import std.stdio, std.concurrency;

 void f1() {
     writeln("owner: ", ownerTid);
     writeln("worker: ", thisTid);
 }

 void main() {
     writeln("owner: ", thisTid);
     writeln("worker: ", spawn(&f1));
 }

 Gives me the following result:

 owner: Tid(18fd58)
 worker: Tid(18fd58)
 owner: Tid(24afe38)
 worker: Tid(24afe38)

 Is it correct? My expectation was that:
 - thisTid called from main will be the same as ownerTid called from f1
 - thisTid called from f1 will be the same as value returned by spawn()

 Thank you for your help.
Feb 09 2016