digitalmars.D.learn - how to use shared keyword in 2.063 version?
- Andrey (29/29) May 31 2013 Hello!
- Anthony Goins (5/34) May 31 2013 To create a shared object you need shared this ctor.
- Jonathan M Davis (3/10) May 31 2013 Either that or you create it as thread-local and cast to shared.
- Andrey (6/16) Jun 01 2013 Does it mean, that to create shared Mutex or shared Socket for
- Jonathan M Davis (9/29) Jun 01 2013 Given the lack of shared constructors, yes - though you should probably ...
- Andrey (2/36) Jun 03 2013 Thank you! Now my app works fine.
Hello! I'm trying to use following code: <======================================> //... class A { private { int m_someVar = 10; } public { this() { } } } int main(string[] args) { shared A a = null; a = new shared(A)(); // error. return 0; } <======================================> And on compile time, the compiler says "Error: non-shared method main.A.this is not callable using a shared object". How can I use an objects as shared, which classes were not defined with "synchronized" or "shared" keyword? Thanks.
May 31 2013
On Friday, 31 May 2013 at 21:01:49 UTC, Andrey wrote:Hello! I'm trying to use following code: <======================================> //... class A { private { int m_someVar = 10; } public { this() { } } } int main(string[] args) { shared A a = null; a = new shared(A)(); // error. return 0; } <======================================> And on compile time, the compiler says "Error: non-shared method main.A.this is not callable using a shared object". How can I use an objects as shared, which classes were not defined with "synchronized" or "shared" keyword? Thanks.To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.
May 31 2013
On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
May 31 2013
On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis wrote:On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex()); ??To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Jun 01 2013
On Saturday, June 01, 2013 10:03:28 Andrey wrote:On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis wrote:Given the lack of shared constructors, yes - though you should probably write it more like auto mutex = cast(shared)new Mutex; auto socket = cast(shared)new Socket; since then you don't have to worry about accidentally changing the base type (like you did with the Socket), and you don't have to write the type multiple times. - Jonathan M DavisOn Friday, May 31, 2013 23:26:19 Anthony Goins wrote:Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex());To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Jun 01 2013
On Saturday, 1 June 2013 at 16:00:58 UTC, Jonathan M Davis wrote:On Saturday, June 01, 2013 10:03:28 Andrey wrote:Thank you! Now my app works fine.On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis wrote:Given the lack of shared constructors, yes - though you should probably write it more like auto mutex = cast(shared)new Mutex; auto socket = cast(shared)new Socket; since then you don't have to worry about accidentally changing the base type (like you did with the Socket), and you don't have to write the type multiple times. - Jonathan M DavisOn Friday, May 31, 2013 23:26:19 Anthony Goins wrote:Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex());To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Jun 03 2013