scope(exit)
MM
MM_member at pathlink.com
Fri May 12 20:09:36 PDT 2006
>Personally, I find the basic guarantee to be of very limited use. For
>most operations, the weak guarantee seems far more appropriate, and I
>believe it is reasonable to expect that of the lock function. In other
>words, if the lock function fails then I would expect m to be in the
>state it was in prior to the lock call. Further, it's possible that
>unlock might consider being asked to unlock a mutex that the calling
>thread doesn't own to be an error condition, which may result in an
>exception being generated. Therefore, if lock fails and throws and
>exception and scope(exit) unlock(m) therefore fails and throws an
>exception as well, the application will terminate because it has two
>exceptions in flight simultaneously. Therefore:
>
> lock(m);
> scope(exit) unlock(m);
>
>is preferred because unlock will only be called if a lock was
>successfully obtained, and will be called regardless of whether the
>following code generates an error or completes successfully.
>
>
>Sean
Lock && unlock could ofcourse be any arbitrary function.. thus I was asking
myself, why use the weak guarantee? When you take the change on a fault in lock
at about the same as unlock your reasoning would be sound. But most of the
time(in my code:) the 'unlock' function is far less complex than the 'lock'
function.
Is it then still better to use the weak guarantee?(not a retoric question... :)
More information about the Digitalmars-d
mailing list