How to use core.thread.Thread

byron via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 07:14:40 PDT 2015


On Friday, 17 July 2015 at 07:56:48 UTC, aki wrote:
> On Thursday, 16 July 2015 at 09:17:47 UTC, Daniel Kozák wrote:
>> class DerivedThread : Thread {
>>     shared int count = 0;
>> }
>
> I thought shared is only for whole of the object.
>     auto thr = new DerivedThread();
> Here, "thr" is not shared but it's member thr.count is shared?
> But if it's not shared, thr object referred by main thread and
> "this" reference of DerivedThread.run() are different?
> What happen if there are both shared and unshared member exists?
>
> class DerivedThread : Thread {
>     shared int count = 0;
>     int foo = 0;
> }
>
> Umm. I can't imagine what's the memory layout of this object.

Since I have yet to use or see anyone use shared in a useful way 
I avoid it.  But I normally think of it like const (not 
immutable).  Shared wont change the memory layout (last time I 
checked) its purpose is meant to help you write correct threaded 
code, ie make sure you synchronize access to it, its not synced 
for you.  You should read the chapter from Andrie's book.

There is also a lot of good stuff in the std lib the helps avoid 
doing things manually.


More information about the Digitalmars-d-learn mailing list