May 11, 2014
On Sunday, 11 May 2014 at 07:31:10 UTC, FreeSlave wrote:
> On Friday, 9 May 2014 at 21:42:14 UTC, Vlad Levenfeld wrote:
>> Is this still the case if the method is const or pure?
>
> Const methods still require synchronization, because other threads may change some data, needed by const method while method is executed, and then you may get wrong results.
>
> Consider:
>
> class Point
> {
> public:
>     float x;
>     float y;
>
>
> }

I send before I end(

class Point
{
public:
    float x;
    float y;

    Tuple!(float, float) getLengthAndAngle() const
    {
        float l = sqrt(x*x+y*y);
        //other thread change x or y
        float a = atan2(x, y);
        return tuple(l, a);
    }
}

May 11, 2014
On Friday, 9 May 2014 at 21:37:37 UTC, Vlad Levenfeld wrote:
> Error: non-shared const method is not callable using a shared mutable object
>
> Why not? If the method is const, it can't modify the object anyway.

Because thread-safety isn't only a problem when writing to memory, reads must also be carefully dealt with.
May 11, 2014
On Friday, 9 May 2014 at 21:58:41 UTC, Steven Schveighoffer wrote:
> On Fri, 09 May 2014 17:45:37 -0400, Vlad Levenfeld <vlevenfeld@gmail.com> wrote:
>
>> Is there any way to declare a method as "safe regardless of shared/mutability/etc" (or some other way to avoid cast(Type)object.property every time I want to check a property which won't affect any state)?
>
> Not really for shared. For everything else, there's const for value properties, and inout for reference properties.
>
> Shared is quite different, because the method has to be cognizant of race conditions. It has to be implemented differently.
>
> Casting away shared is somewhat dangerous, but OK if you logically know there is no race condition.
>
> -Steve

You could just declare the function as "immutable".

But then, it'll only work on immutable types.

But then again, that would be the only way to actually statically and safely guarantee there are no race conditions.
May 13, 2014
@FreeSlave & John Colvin

Yes, I see your point. I could still get tearing on a read. So, in the case of methods that I believe are safe (e.g. 1-line @property getters) I'll just write a shared variadic function template that uses (cast()this).foo(args) to forward to the non-shared method... and in less-safe cases, I can just add some synchronization code to the shared version.
Or maybe its safer to do it the other way around, i.e. (cast(shared)this) and forward from non-shared to shared? There's not yet any established best practices for this, are there?
1 2
Next ›   Last »