www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is core.thread.osthread.Thread thread-safe?

reply tsbockman <thomas.bockman gmail.com> writes:
I want to do this:

import core.thread.osthread : Thread;

void main() {
     shared(Thread) thread1 = new Thread({
         // Do stuff for a while...
     });

     auto thread2 = new Thread({
        // ...
        if(thread1.isRunning)
            // Do a thing.
        else
            // Do a different thing.
        // ...
    });

     auto thread3 = new Thread({
        // ...
        if(thread1.isRunning)
            // Do a thing.
        else
            // Do a different thing.
        // ...
    });
}

However, I get various compile-time errors when I try to write 
code like this because nothing in the Thread API is annotated 
`shared`. Are Thread objects really not thread-safe, and if so 
what is the correct way to synchronize access to one?

(The documentation for core.thread is broken right now, with dead 
links and at least one reference to an example that doesn't 
actually appear anywhere on the page.)
Dec 05 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 5 December 2020 at 18:48:19 UTC, tsbockman wrote:
 (The documentation for core.thread is broken right now, with 
 dead links and at least one reference to an example that 
 doesn't actually appear anywhere on the page.)
im not sure about your other question but use my docs they actually work (...for the most part lol) http://dpldocs.info/core.thread I don't personally use the shared modifier here, you can just access stuff without it like in the source it does thigns like atomicLoad so like i think it is ok but idk eally.
Dec 05 2020
parent tsbockman <thomas.bockman gmail.com> writes:
On Saturday, 5 December 2020 at 18:58:13 UTC, Adam D. Ruppe wrote:
 On Saturday, 5 December 2020 at 18:48:19 UTC, tsbockman wrote:
 (The documentation for core.thread is broken right now, with 
 dead links and at least one reference to an example that 
 doesn't actually appear anywhere on the page.)
im not sure about your other question but use my docs they actually work (...for the most part lol) http://dpldocs.info/core.thread
Yes, yours are much better - they have a bunch of content which is mysteriously missing from the official docs build. Thanks!
Dec 05 2020