How to use core.thread.Thread

maarten van damme via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 16 01:21:18 PDT 2015


Have you checked out std.parallelism and std.concurrency?

2015-07-16 9:57 GMT+02:00 aki via Digitalmars-d-learn <
digitalmars-d-learn at puremagic.com>:

> I can't resolve the compile errors:
>
> import core.thread;
> class DerivedThread : Thread {
>         int count = 0;
>         this() {
>                 super(&run);
>         }
>         private void run() {
>                 inc();  //testThread.d(8): Error: shared method
> testThread.DerivedThread.inc is not callable using a non-shared object
>         }
>         synchronized void inc() {
>                 ++count;        //testThread.d(11): Deprecation:
> read-modify-write operations are not allowed for shared variables. Use
> core.atomic.atomicOp!"+="(this.count, 1) instead.
>         }
> }
> void main() {
>         auto thr = new DerivedThread();
>         thr.start();
>         thr.inc();      //testThread.d(17): Error: shared method
> testThread.DerivedThread.inc is not callable using a non-shared object
>         thr.join();
> }
>
>
> 1. Should I declare thr as shared? But
>         auto thr = new shared DerivedThread();
> does not resolve it.
>
> 2. Why "++count" cause an error? I think it is safe because
> it is marked as synchronized. If it is forced to use atomicOp
> all the time, it's painful.
>
> 3. Are there any tutorials about using Thread class?
>
> Aki.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150716/7a4faa74/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list