Click here to Skip to main content
15,920,111 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Need to kill thread when application is closed Pin
Amit Sk Sharma29-Mar-11 2:22
Amit Sk Sharma29-Mar-11 2:22 
GeneralRe: Need to kill thread when application is closed Pin
Luc Pattyn29-Mar-11 2:24
sitebuilderLuc Pattyn29-Mar-11 2:24 
QuestionLoad Report Failed is coming if i run Windows application exe in Windows 7 OS but it is working fine in Windows XP [modified] Pin
sr15928-Mar-11 23:17
sr15928-Mar-11 23:17 
AnswerRe: Load Report Failed is coming in Windows application in Windows 7 OS but it is working fine in Windows XP Pin
Dave Kreskowiak29-Mar-11 2:00
mveDave Kreskowiak29-Mar-11 2:00 
GeneralRe: Load Report Failed is coming in Windows application in Windows 7 OS but it is working fine in Windows XP Pin
sr15929-Mar-11 2:47
sr15929-Mar-11 2:47 
GeneralRe: Load Report Failed is coming in Windows application in Windows 7 OS but it is working fine in Windows XP Pin
Dave Kreskowiak29-Mar-11 12:26
mveDave Kreskowiak29-Mar-11 12:26 
AnswerRe: Load Report Failed is coming if i run Windows application exe in Windows 7 OS but it is working fine in Windows XP Pin
Bernhard Hiller31-Mar-11 22:44
Bernhard Hiller31-Mar-11 22:44 
QuestionSlow property initialization and thread safety Pin
Bernhard Hiller27-Mar-11 20:43
Bernhard Hiller27-Mar-11 20:43 
When the initialization of a property takes a lot of time, and the property could be accessed from several threads, we use a lock statement around the initialization code. It looks something like this:
private object _BerniesLock = new object();
private List<Bernie> _Bernies;
public List<Bernie> Bernies
{
    get
    {
        lock (_BerniesLock)
        {
            if (_Bernies == null)
            {
                _Bernies = new List<Bernie>();
                //here comes the initialization
            }
        }
        return _Bernies;
    }
}

But thus we have to create the lock whenever we access the property. Actually, we need it only when we do the initialization (let's assume here that the list would not be changed after initialization). And locks are said to cost some performance.
Consequently, I thought I could optimize that to:
private object _BerniesLock = new object();
private List<Bernie> _Bernies;
public List<Bernie> Bernies
{
    get
    {
        if (_Bernies == null)
        {
            lock (_BerniesLock)
            {
                if (_Bernies == null)
                {
                    _Bernies = CreateBernies();
                }
            }
        }
        return _Bernies;
    }
}
private List<Bernie> CreateBernies()
{
    List<Bernie> retVal = new List<Bernie>();
    //here comes the initialization
    return retVal;
}

When the member variable is not null, I can return it. When it is null, create the lock, check again if it is not null (a different thread may have created the list meanwhile), and if necessary create the list. The list is created in a different function, otherwise the list could exist without having been filled properly, and cause some exceptions when iterating while it is still being filled.

Looking at the code, it looks as if it could work.
But I have some doubts.

What is the state of the member variable _Bernies when the function CreateBernies() is being executed? Is it really still null? I remember the times when I was programming in C++ (VC++ 6), when invalid pointers were a terrible thing to debug: pointers which were not null, but pointed to just somewhere (typically 0xcdcdcdcd). Is the implementation of the .NET framework safe here?

And next, there exists a volatile key word. It is used to prevent caching, when the variable is accessed it is always freshly read from memory. When I look at the situation above, the second time the member variable is checked for null would be a perfect candidate for the use of volatile, i.e.
private volatile List<Bernie> _Bernies;

But volatile costs a lot of performance too, perhaps even more than lock...

In the concrete case I can live well with the standard solution. But having stumbled upon these issues, I am now curious to learn more, and I am looking forward to an interesting discussion.
AnswerRe: Slow property initialization and thread safety Pin
Luc Pattyn27-Mar-11 22:48
sitebuilderLuc Pattyn27-Mar-11 22:48 
GeneralRe: Slow property initialization and thread safety Pin
Bernhard Hiller28-Mar-11 20:14
Bernhard Hiller28-Mar-11 20:14 
GeneralRe: Slow property initialization and thread safety Pin
Luc Pattyn28-Mar-11 22:05
sitebuilderLuc Pattyn28-Mar-11 22:05 
AnswerRe: Slow property initialization and thread safety Pin
davidnz28-Mar-11 0:34
davidnz28-Mar-11 0:34 
GeneralRe: Slow property initialization and thread safety [modified] Pin
Not Active28-Mar-11 18:13
mentorNot Active28-Mar-11 18:13 
GeneralRe: Slow property initialization and thread safety Pin
davidnz28-Mar-11 20:16
davidnz28-Mar-11 20:16 
GeneralRe: Slow property initialization and thread safety Pin
Bernhard Hiller28-Mar-11 20:19
Bernhard Hiller28-Mar-11 20:19 
GeneralRe: Slow property initialization and thread safety Pin
davidnz28-Mar-11 20:23
davidnz28-Mar-11 20:23 
QuestionFatal Execution Engine Error while trying to run .net exe Pin
surender.m22-Mar-11 20:38
surender.m22-Mar-11 20:38 
AnswerRe: Fatal Execution Engine Error while trying to run .net exe Pin
Pete O'Hanlon22-Mar-11 23:04
mvePete O'Hanlon22-Mar-11 23:04 
GeneralRe: Fatal Execution Engine Error while trying to run .net exe Pin
surender.m22-Mar-11 23:52
surender.m22-Mar-11 23:52 
GeneralRe: Fatal Execution Engine Error while trying to run .net exe Pin
Pete O'Hanlon23-Mar-11 0:01
mvePete O'Hanlon23-Mar-11 0:01 
GeneralRe: Fatal Execution Engine Error while trying to run .net exe Pin
surender.m23-Mar-11 0:30
surender.m23-Mar-11 0:30 
GeneralRe: Fatal Execution Engine Error while trying to run .net exe Pin
Pete O'Hanlon23-Mar-11 0:42
mvePete O'Hanlon23-Mar-11 0:42 
AnswerRe: Fatal Execution Engine Error while trying to run .net exe Pin
Prasanta_Prince15-Apr-11 0:17
Prasanta_Prince15-Apr-11 0:17 
QuestionBest practices on Generics (in .NET 4.0)? Pin
Sander Rossel22-Mar-11 11:09
professionalSander Rossel22-Mar-11 11:09 
AnswerRe: Best practices on Generics (in .NET 4.0)? Pin
DaveyM6922-Mar-11 12:07
professionalDaveyM6922-Mar-11 12: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.