On Thursday, 18 August 2022 at 11:18:18 UTC, frame wrote:
> It does not - which compiler version are you using?
Error: shared
method app.A.this
is not callable using a non-shared object
v2.100.0
> And it shouldn't - shared (atomic lock free) and synchronized (locking) are different concepts.
shared
means that data can be accessed by different threads, indeed when you want to send a pointer to another thread the pointed data must be shared
even if it's a synchronized
class. For example the following code
import std.stdio;
import std.concurrency;
synchronized class A{
private:
int a;
public:
this() pure {
a = 1;
}
int getA() pure{
return a;
}
}
void thread(A a){
writeln(a.getA());
}
int main(){
auto c = new A();
spawn(&thread, c);
return 0;
}
won't compile
main.d(18): Error: `shared` method `main.A.getA` is not callable using a non-shared object
/usr/include/dlang/dmd/std/concurrency.d(519): Error: static assert: "Aliases to mutable thread-local data not allowed."
main.d(23): instantiated from here: `spawn!(void function(A), A)`
if you want to compile it your class must be shared
import std.stdio;
import std.concurrency;
synchronized class A{
private:
int a;
public:
this() pure {
a = 1;
}
int getA() pure{
return a;
}
}
void thread(shared A a){
writeln(a.getA());
}
int main(){
auto c = new shared A();
spawn(&thread, c);
return 0;
}