Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
January 13, 2015 Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
I have written the following code: test.d ============================== import core.thread; import std.stdio; void threadFunc(){ writeln("Thread func"); } public static this(){ auto t = new Thread( &threadFunc ); t.start(); writeln("Static init"); } void main(){ writeln("End of main"); } run ====================== rdmd test.d result ====================== Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Sta... Is this normal, what's happening? |
January 13, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On Tuesday, 13 January 2015 at 13:53:11 UTC, tcak wrote:
> I have written the following code:
>
> test.d
> ==============================
> import core.thread;
> import std.stdio;
>
> void threadFunc(){
> writeln("Thread func");
> }
>
> public static this(){
> auto t = new Thread( &threadFunc );
> t.start();
>
> writeln("Static init");
> }
>
> void main(){
> writeln("End of main");
> }
>
>
> run
> ======================
> rdmd test.d
>
>
> result
> ======================
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Static init
> Thread func
> Sta...
>
> Is this normal, what's happening?
When I defined static init with shared
public shared static this()
it works normal now. But it doesn't explain above issue. What's the relation between a new thread and a module's initialiser?
|
January 13, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | V Tue, 13 Jan 2015 13:53:09 +0000 tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > I have written the following code: > > test.d > ============================== > import core.thread; > import std.stdio; > > void threadFunc(){ > writeln("Thread func"); > } > > public static this(){ > auto t = new Thread( &threadFunc ); > t.start(); > > writeln("Static init"); > } > > void main(){ > writeln("End of main"); > } > > > run > ====================== > rdmd test.d > > > result > ====================== > Static init > Thread func > Static init > Thread func > Static init > Thread func > Static init > Thread func > Static init > Thread func > Static init > Thread func > Static init > Thread func > Static init > Thread func > Sta... > > Is this normal, what's happening? try shared static this(){ ... instead of public static this(){ |
January 13, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak Attachments: | On Tue, 13 Jan 2015 13:56:05 +0000 tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > it works normal now. But it doesn't explain above issue. What's the relation between a new thread and a module's initialiser? yes. |
January 13, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | V Tue, 13 Jan 2015 13:56:05 +0000 tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Tuesday, 13 January 2015 at 13:53:11 UTC, tcak wrote: > > I have written the following code: > > > > test.d > > ============================== > > import core.thread; > > import std.stdio; > > > > void threadFunc(){ > > writeln("Thread func"); > > } > > > > public static this(){ > > auto t = new Thread( &threadFunc ); > > t.start(); > > > > writeln("Static init"); > > } > > > > void main(){ > > writeln("End of main"); > > } > > > > > > run > > ====================== > > rdmd test.d > > > > > > result > > ====================== > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Static init > > Thread func > > Sta... > > > > Is this normal, what's happening? > > When I defined static init with shared > > public shared static this() > > it works normal now. But it doesn't explain above issue. What's the relation between a new thread and a module's initialiser? I am not sure but my guess is static this needs to be called before anything else in module so when you try call threadFunc it looks if static this has been called and finished which is not true so it call it again |
January 13, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | On Tuesday, 13 January 2015 at 14:02:45 UTC, Daniel Kozák via Digitalmars-d-learn wrote: > V Tue, 13 Jan 2015 13:56:05 +0000 > tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> > napsáno: > >> On Tuesday, 13 January 2015 at 13:53:11 UTC, tcak wrote: >> > I have written the following code: >> > >> > test.d >> > ============================== >> > import core.thread; >> > import std.stdio; >> > >> > void threadFunc(){ >> > writeln("Thread func"); >> > } >> > >> > public static this(){ >> > auto t = new Thread( &threadFunc ); >> > t.start(); >> > >> > writeln("Static init"); >> > } >> > >> > void main(){ >> > writeln("End of main"); >> > } >> > >> > >> > run >> > ====================== >> > rdmd test.d >> > >> > >> > result >> > ====================== >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Static init >> > Thread func >> > Sta... >> > >> > Is this normal, what's happening? >> >> When I defined static init with shared >> >> public shared static this() >> >> it works normal now. But it doesn't explain above issue. What's the relation between a new thread and a module's initialiser? > > I am not sure but my guess is > > static this needs to be called before anything else in module so when > you try call threadFunc it looks if static this has been called and > finished which is not true so it call it again And here is better explanation http://dlang.org/module.html#staticorder Static constructors are code that gets executed to initialize a module or a class before the main() function gets called. ... Static constructors and static destructors run on thread local data, and are run whenever threads are created or destroyed. |
January 15, 2015 Re: Endless static this call when used a thread in it | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On 1/13/15 8:56 AM, tcak wrote:
>
> When I defined static init with shared
>
> public shared static this()
>
> it works normal now. But it doesn't explain above issue. What's the
> relation between a new thread and a module's initialiser?
In case it's not clear -- shared static this runs ONCE at the beginning of the program (before main() starts), static this runs for EVERY thread that starts, before the thread starts execution.
The reason is that "static this" initializes thread local data. "shared static this" initializes global data (i.e. shared and __gshared).
Hope this helps.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation