Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:57:00+00:00 2026-05-22T16:57:00+00:00

The documentation for LazyThreadSafetyMode states that using the value ExecutionAndPublication could cause deadlocks if

  • 0

The documentation for LazyThreadSafetyMode states that using the value ExecutionAndPublication could cause deadlocks if the initialization method (or the default constructor, if there is no initialization method) uses locks internally. I am trying to get a better understanding of examples that could cause a deadlock when using this value. In my use of this value, I am initializing a ChannelFactory. I cannot see the ChannelFactory’s constructor using any internal locks (reviewing the class with Reflector), so I believe this scenario does not fit the possible deadlock situation, but I am curious what situations could cause a deadlock as well as if there could be a possible deadlock initializing the ChannelFactory.

So, to summarize, my questions are:

  1. Is it possible to cause a deadlock initializing the ChannelFactory using ExecutionAndPublication?

  2. What are some possible ways to cause a deadlock initializing other objects using ExecutionAndPublication?

Suppose you have the following code:

class x
{
   static Lazy<ChannelFactory<ISomeChannel>> lcf = 
        new Lazy<ChannelFactory<ISomeChannel>>(
        () => new ChannelFactory<ISomeChannel>("someEndPointConfig"), 
        LazyThreadSafetyMode.ExecutionAndPublication
        );

    public static ISomeChannel Create()
    {
        return lcf.Value.CreateChannel();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:57:01+00:00Added an answer on May 22, 2026 at 4:57 pm
    1. It’s as documented – if it doesn’t use any locks, this usage cannot cause any deadlocks.
    2. Imagine that you have a lazy value that you initialize by reading from a database, but you want to make sure that only one thread is accessing the DB at any moment. If you have other code that accesses the DB, you could have a deadlock. Consider the following code:
    void Main()
    {
        Task otherThread = Task.Factory.StartNew(() => UpdateDb(43));
        Thread.Sleep(100);
        Console.WriteLine(lazyInt.Value);
    }
    
    static object l = new object();
    Lazy<int> lazyInt = new Lazy<int>(Init, LazyThreadSafetyMode.ExecutionAndPublication);
    
    static int Init()
    {
        lock(l)
        {
            return ReadFromDb();
        }
    }
    
    void UpdateDb(int newValue)
    {
        lock(l)
        {
            // to make sure deadlock occurs every time
            Thread.Sleep(1000);
    
            if (newValue != lazyInt.Value)
            {
                // some code that requires the lock
            }
        }
    }
    

    Init() reads from the DB, so it has to use the lock. UpdateDb() writes to the DB, so it needs the lock too, and since Lazy uses a lock internally too in this case, it causes deadlock.

    In this case, it would be easy to fix the deadlock by moving the access to lazyInt.Value in UpdateDb() outside the lock statement, but it may not be so trivial (or obvious) in other cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Documentation states that You must use /EHa when using _set_se_translator.. My question is: should
The documentation states that by default, the (WebApplication?) config file of the DispatcherServlet is
Documentation for the constructor new Boolean(boolean value) in Java states: Note: It is rarely
The documentation for CFindFile states that Nonzero if there are more files; zero if
Documentation simply states that setting setAutosavingDelay to anything > 0 on the shared doc
The documentation for the round() function states that you pass it a number, and
Documentation states that interface delegation is available for Win32 only. Currently I can't test
The documentation for NSDictionary says that the method objectForKey returns nil if the NSDictionary
The documentation says there's a limit to the number of regions that can be
The documentation for set local states: Note that SET LOCAL will appear to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.