Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: returning when inside a lock - is it safe? Pin
Brandon Haase13-Jan-04 16:13
Brandon Haase13-Jan-04 16:13 
AnswerRe: returning when inside a lock - is it safe? Pin
scadaguy13-Jan-04 5:23
scadaguy13-Jan-04 5:23 
Brandon,

Returning inside a lock is okay.

I don't necessarily see anything incorrect about your implementation. However, the double-checked locking pattern is essentially useless as you have used it since you'll always be aquiring at least one lock anyway. I would suggest the following code...

public class IsoCurrencyInfo
{
    private static Hashtable _iciRegistry = new Hashtable();

    public static IsoCurrencyInfo GetInstance(string isoCode)
    {
        if (!_iciRegistry.Contains(isoCode))
        {
            lock (syncRoot)
            {
                if (!_iciRegistry.Contains(isoCode))
                {
                    _iciRegistry.Add(isoCode, new IsoCurrencyInfo(isoCode));
                }
            }
        }
        return (IsoCurrencyInfo)_iciRegistry[isoCode];
    }
}


It is important to realize that the Hashtable is the only IDictionary collection that will work as I have used it. This is because the Hashtable can support multiple readers and one writer simultaneously. Likewise, you won't be able to use the same trick with an ArrayList. In this implementation the lock is rarely acquired.

Brian
GeneralRe: returning when inside a lock - is it safe? Pin
Brandon Haase13-Jan-04 16:28
Brandon Haase13-Jan-04 16:28 
Generalremoting access Pin
haih12-Jan-04 15:23
haih12-Jan-04 15:23 
GeneralRe: remoting access Pin
Heath Stewart13-Jan-04 4:31
protectorHeath Stewart13-Jan-04 4:31 
GeneralHelp with MonthCalendar class Pin
KingTermite12-Jan-04 15:18
KingTermite12-Jan-04 15:18 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 3:41
protectorHeath Stewart13-Jan-04 3:41 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 4:27
KingTermite13-Jan-04 4:27 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 4:37
protectorHeath Stewart13-Jan-04 4:37 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 5:43
KingTermite13-Jan-04 5:43 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 5:50
protectorHeath Stewart13-Jan-04 5:50 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 6:02
KingTermite13-Jan-04 6:02 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 6:47
protectorHeath Stewart13-Jan-04 6:47 
GeneralRe: Help with MonthCalendar class Pin
KingTermite15-Jan-04 14:49
KingTermite15-Jan-04 14:49 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart16-Jan-04 3:42
protectorHeath Stewart16-Jan-04 3:42 
GeneralFile Permissions Pin
krisp12-Jan-04 14:54
krisp12-Jan-04 14:54 
GeneralRe: File Permissions Pin
Jeff Mackie12-Jan-04 17:33
sussJeff Mackie12-Jan-04 17:33 
GeneralRe: File Permissions Pin
krisp12-Jan-04 17:36
krisp12-Jan-04 17:36 
GeneralRe: File Permissions Pin
Jeff Mackie12-Jan-04 17:48
sussJeff Mackie12-Jan-04 17:48 
GeneralRe: File Permissions Pin
Heath Stewart13-Jan-04 2:57
protectorHeath Stewart13-Jan-04 2:57 
QuestionHow can I use the Cabinet.dll in c# Pin
aw1ay12-Jan-04 14:32
aw1ay12-Jan-04 14:32 
AnswerRe: How can I use the Cabinet.dll in c# Pin
Heath Stewart13-Jan-04 2:52
protectorHeath Stewart13-Jan-04 2:52 
GeneralRe: How can I use the Cabinet.dll in c# Pin
aw1ay13-Jan-04 15:52
aw1ay13-Jan-04 15:52 
GeneralRe: How can I use the Cabinet.dll in c# Pin
Heath Stewart14-Jan-04 3:22
protectorHeath Stewart14-Jan-04 3:22 
Generalpassing data to installer class Pin
econnor12-Jan-04 11:07
econnor12-Jan-04 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.