Thread overview | |||||
---|---|---|---|---|---|
|
June 29, 2013 Passing a instance of a class to a thread via spawn? | ||||
---|---|---|---|---|
| ||||
I want to spawn a few worker threads to do some processing but i want the spawned function to have access to a shared object. How do i accomplish this? I'm currently passing it as a parameter and getting the error: "Aliases to mutable thread-local data not allowed." |
June 29, 2013 Re: Passing a instance of a class to a thread via spawn? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On 06/29/2013 01:35 PM, Gary Willoughby wrote:
> I want to spawn a few worker threads to do some processing but i want
> the spawned function to have access to a shared object. How do i
> accomplish this?
>
> I'm currently passing it as a parameter and getting the error: "Aliases
> to mutable thread-local data not allowed."
You must pass a shared object:
import std.stdio;
import std.concurrency;
import core.thread;
class C
{
void foo_shared() shared
{}
void bar_notshared() const
{}
}
void worker(shared(C) c)
{
c.foo_shared();
// Does not compile:
// c.bar_notshared();
}
void main()
{
auto c = new shared(C)();
spawn(&worker, c);
thread_joinAll();
}
Ali
|
June 30, 2013 Re: Passing a instance of a class to a thread via spawn? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > You must pass a shared object:
Thanks!
|
Copyright © 1999-2021 by the D Language Foundation