shared and cryptic error messages

Jose Armando Garcia jsancio at gmail.com
Thu Mar 31 12:27:38 PDT 2011


On Wed, Mar 30, 2011 at 9:39 PM, Jason House
<jason.james.house at gmail.com> wrote:
> Jose Armando Garcia Wrote:
>
>> Why am I getting this error? I suspect that synchronized is the
>> problem.
>
> A synchronized class is implicitly shared and most of the methods are synchronized.  I say most because at a minimum, the constructor isn't synchronized on anything.  As you probably know shared is transitive, so all members are implicitly shared as well.
>
>
>> How do I get around this error?
>
> That's not easy to answer...  To get the compiler to shut up, you can copy and paste FILE's destructor and mark it as shared.  Of course, every method call to your file will similarly required shared methods.  If you make no copies of the file object, and all accesses are from within your synchronized methods, you can probably cast away shared while making the method calls.  Of course, the common theme here is that you're verifying the multi-threaded safety of all this stuff along the way.  If you're not comfortable doing that, then you probably want to do another design that you can prove works.
>
>

This is a great suggestion and that is what I do in my implementation
of the synchronized class but I can't cast away the call to the dtor.
That call is done by the compiler. I am going to look into __gshared.

>> Does this mean that
>> synchronized classes are not allowed to have as member
>> unsynchronized/regular classes?
>
> As I said above, shared is transitive.  It's implicit that all members are shared even though you don't mark them as such.  No shared objects can have non-shared members.  If there are parts that are not accessed by more than one thread, you can either restructure your code a bit, or make liberal use of casts.
>
>
>> Has anyone tried to write a
>> multithreaded application using D?
>
> Yes, and even succeeded :)
>
>

Which? And it it cannot be named what does it do? How many lines of code?

>> Why is the error showing up in
>> std/stdio.d and not in my file?
>
> It's an awful error message.  I complained about it back in 2009: http://d.puremagic.com/issues/show_bug.cgi?id=3642
>
> When this kind of thing pops up, it's either misusing the object (shouldn't be shared), or the object is missing a shared method.  The error is really awful.  After finding this case, I spotted a few others.  This error message is extremely generic and used in many other cases.  In more mature areas of the code, the compiler issues a much more helpful message on the very next line.  For shared, there's no specialized error message and it leaves the user scratching their head...
>
>
>> Can anyone point me to a decent
>> documentation of how "shared" works? I have read chapter 13 of The D
>> Programming Language and it is not sufficient for the level of
>> understanding I seek.
>
> Nope.  D is notorious for having no specs or other documentation to give a detailed understanding.  I would have assumed TDPL was the best place.  (I don't have a copy)
>
>
>> That is a lot of question. Hopefully I get answer to at least one.
>
>


More information about the Digitalmars-d mailing list