#facepalm of the day

of the day

ListA.Lock;

try

// do stuff with List A

finally

ListB.Unlock

end;

13 thoughts on “#facepalm of the day


  1. Lars Fosdal Well the main idea is that you can’t access/manipulate the list items except through the interface returned from the Lock() method. The “synchronized access interface” will unlock when it goes out of scope.


    So in your case, the code would become:


    var


    listAccess: IListAccess;


    begin


    listAccess := ListA.Lock();


    listAccess.Add(…); // or whatever you want


    end; // list is unlocked when listAccess goes out of scope or is nilled


    This way you cannot lock the wrong list (compared to the one you’re manipulating).


    So IListAccess would have most of the methods, while IThreadSafeList (or whatever ListA is) would basically just have Lock() and possibly some helper methods if needed.


    Guess maybe a generic wrapper could be possible with a bit of voodoo.


  2. If I could remember that one other language that has something like this using keyword which basically just is syntax for try/finally something…


  3. Language support is the way here. Heap allocation of an object to implement an interface is overkill. Or you could just get the code right in the first place. I’d take that over darn ref counting.

Leave a Reply