On 2018-10-15 20:46, Manu wrote:

1. traditional; assert that the object become thread-local by
acquiring a lock, cast shared away

Instead of having to explicitly cast away shared we could leverage the synchronized statement. It could be enhanced to allow the following:

shared int a;
Mutex mutex;

synchronized(tlsA; a, mutex)
{
    // within this scope "tlsA" is the same as "a" but without
    // the "shared" qualifier. "tlsA" is not allowed to escape the block
}

This could also be implemented as a library function, but that would require casting away shared inside the implementation:

guard(a, mutex, (tlsA){

});

void guard(T)(scope shared T value, Mutex mutex, scope void delegate (scope T) block)
{
    mutex.lock_nothrow();
    scope (exit)
        mutex.unlock_nothrow();

    block(cast(T) value);
}


--
/Jacob Carlborg

Reply via email to