| Thread overview | |||||
|---|---|---|---|---|---|
|
July 18, 2013 Passing a class instance to a thread via spawn() | ||||
|---|---|---|---|---|
| ||||
Hello all,
I have a data structure which is a final class. Once created, the contents of the class will not be mutated (only its const methods will be called).
Is there any way to pass this to a thread via spawn() or via message-passing? I've seen instructions to use shared() but if I try and declare the class this way I get errors such as "non-shared method is not callable using a shared object" and "non-shared const method is not callable using a shared object".
The ideal would be to mark this object, once created, as immutable -- but trusty methods like assumeUnique() don't work for classes!
Can anyone advise?
Thanks & best wishes,
-- Joe
| ||||
July 18, 2013 Re: Passing a class instance to a thread via spawn() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | On 07/18/2013 04:23 PM, Joseph Rushton Wakeling wrote:
> Hello all,
>
> I have a data structure which is a final class. Once created, the contents of
> the class will not be mutated (only its const methods will be called).
>
> Is there any way to pass this to a thread via spawn() or via message-passing?
> I've seen instructions to use shared() but if I try and declare the class this
> way I get errors such as "non-shared method is not callable using a shared
> object" and "non-shared const method is not callable using a shared object".
>
> The ideal would be to mark this object, once created, as immutable -- but trusty
> methods like assumeUnique() don't work for classes!
>
> Can anyone advise?
>
> Thanks & best wishes,
>
> -- Joe
>
The following seems to work but it may have more 'immutable' keywords than you need. For example, the final class F need not be 'immutable'. I think it is the objects of it that you want 'immutable'. (?)
Anyway, the following may be a start:
import std.concurrency;
import core.thread;
interface I
{
int foo();
}
class C : I
{
int i;
this(int i) immutable {
this.i = i;
}
override int foo() {
return 1;
}
}
immutable final class F : C
{
double d;
this(int i, double d) immutable {
super(i);
this.d = d;
}
/* 'override' keyword is required if the class is non-immutable
* and not required otherwise. Bug? */
int foo() {
return 2;
}
int bar() const {
return 3;
}
}
void worker(immutable(F) f)
{
f.bar();
}
void main()
{
auto f = new immutable(F)(42, 1.5);
spawn(&worker, f);
thread_joinAll();
}
Ali
| |||
July 19, 2013 Re: Passing a class instance to a thread via spawn() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, 18 July 2013 at 23:54:22 UTC, Ali Çehreli wrote:
> On 07/18/2013 04:23 PM, Joseph Rushton Wakeling wrote:
>
> > Hello all,
> >
> > I have a data structure which is a final class. Once
> created, the contents of
> > the class will not be mutated (only its const methods will be
> called).
> >
> > Is there any way to pass this to a thread via spawn() or via
> message-passing?
> > I've seen instructions to use shared() but if I try and
> declare the class this
> > way I get errors such as "non-shared method is not callable
> using a shared
> > object" and "non-shared const method is not callable using a
> shared object".
> >
> > The ideal would be to mark this object, once created, as
> immutable -- but trusty
> > methods like assumeUnique() don't work for classes!
> >
> > Can anyone advise?
> >
> > Thanks & best wishes,
> >
> > -- Joe
> >
>
> The following seems to work but it may have more 'immutable' keywords than you need. For example, the final class F need not be 'immutable'. I think it is the objects of it that you want 'immutable'. (?)
>
> Anyway, the following may be a start:
>
> import std.concurrency;
> import core.thread;
>
> interface I
> {
> int foo();
> }
>
> class C : I
> {
> int i;
>
> this(int i) immutable {
> this.i = i;
> }
>
> override int foo() {
> return 1;
> }
> }
>
> immutable final class F : C
> {
> double d;
>
> this(int i, double d) immutable {
> super(i);
> this.d = d;
> }
>
> /* 'override' keyword is required if the class is non-immutable
> * and not required otherwise. Bug? */
> int foo() {
> return 2;
> }
>
> int bar() const {
> return 3;
> }
> }
>
> void worker(immutable(F) f)
> {
> f.bar();
> }
>
> void main()
> {
> auto f = new immutable(F)(42, 1.5);
>
> spawn(&worker, f);
>
> thread_joinAll();
> }
>
> Ali
That's a very interesting idea. Thanks so much for sharing! :-)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply