TDPL, shared data, and Phobos
Graham St Jack
graham.stjack at internode.on.net
Mon Jul 19 13:47:29 PDT 2010
On Sun, 18 Jul 2010 16:05:08 +0000, Sean Kelly wrote:
> Graham St Jack <graham.stjack at internode.on.net> wrote:
>> On Sat, 17 Jul 2010 11:42:03 -0400, Sean Kelly wrote:
>>
>>> The casts are necessary because I haven't yet applied 'shared' to
>>> druntime. I ran into a few issues when doing so and rolled back my
>>> changes. I'll give it another shot before the next release.
>>
>> I'm glad you announced you intention - I was just about to roll up my
>> sleeves and give it a go for Condition, but will wait for the next
>> release.
>>
>> Like all my previous attempts to use shared, I have waited for quite a
>>
>> while for things to improve, tried using shared again, hit a brick wall
>> and resorted to defeating the compiler's detection of shared data.
>>
>> TDPL raised my hopes without actually making it clear how to use
>> synchronized classes. Alas, it seems to me that they still aren't
>> usable
>> in practice. With any luck the problems are just library issues which
>> can
>> be fixed relatively easily.
>>
>> Like Brian Palmer, I am frustrated by the lack of documentation about
>> shared and druntime's sync package, and am happy to lend a hand if that
>> would be helpful.
>>
>> The code I am trying to write is a simple synchronized class with a
>> Condition, but I can't create a Condition on a shared "this".
>>
>> A cut-down version of what I want to write is:
>>
>> synchronized class Foo {
>> Condition mCondition;
>> this() {
>> mCondition = cast(shared) new Condition(this);
>> }
>> void some_method() {
>> }
>> }
>>
>> I realise that Condition wants a Mutex, but a synchronized class
>> already
>> has an implicit one which is implicitly used by all the methods, so the
>> above is definitely what I want to write.
>>
>> What I have to write instead (which avoids the compiler noticing that
>> anything is being shared) is:
>>
>> class Foo {
>> Mutex mMutex;
>> Condition mCondition;
>> this() {
>> mMutex = new Mutex();
>> mCondition = new Condition(mMutex);
>> }
>> void some_method() {
>> synchronized(mMutex) {
>> }
>> }
>> }
>>
>> The latter works just fine, but it is very disappointing after all the
>>
>> fuss about how important shared is that you can't actually use it for
>> the
>> most mainstream of all uses - a synchronized class with a condition
>> (which is what a message queue between threads is supposed to be).
>
> new Mutex(this) makes the mutex the object monitor, so it will be what's
> locked for synchronized functions.
That's cool. I look forward to shared not being an issue ;-)
I assume that when Condition and Mutex are shareable, I will then (from
your other post) write:
synchronized class Foo {
Mutex mMutex;
Condition mCondition;
this() {
mMutex = cast(shared) new Mutex(this);
mCondition = cast(shared) new Condition(mMutex);
}
void some_method() {
...
mCondition.notify; // ok because mMutex is locked
}
}
More information about the Digitalmars-d
mailing list