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

  • Home
  • SEARCH
  • 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 1897950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:42:27+00:00 2026-05-17T06:42:27+00:00

Consider the following test snippet: // act AutoResetEvent workDoneEvent = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(delegate {

  • 0

Consider the following test snippet:

        // act
        AutoResetEvent workDoneEvent = new AutoResetEvent(false);
        ThreadPool.QueueUserWorkItem(delegate
                                         {
                                             ProcessAndSignal(processor, workDoneEvent);
                                         }, null);

        // let worker thread have a go
        workDoneEvent.WaitOne();
        blockingFetcher.WaitForNextMessage = false;

        // assert
        Assert.That(processor.StopCause, Is.Null);
    }

    private static void ProcessAndSignal(MessageProcessor processor, AutoResetEvent workDoneEvent)
    {
        workDoneEvent.Set();
        // this invocation will block until the WaitForNextMessageFlag is set
        processor.ProcessMessages();
    }

Ideal scenario:

  1. ProcessAndSignalMethod is queued on the thread pool but does not start to execute.
  2. The main thread blocks (autoResetEvent.WaitOne())
  3. A worker thread starts to execute the “ProcessAndSignal” method
  4. The worker threads has enough time to signal the flag and start execution of the ProcessMessages method
  5. The main thread is spawned back into life and sets the property which will cause the ProcessAndSignal method to complete gracefully

Can the following scenario occur?

1) ProcessAndSignal() will start to execute before the main thread sets the AutoResetEvent to WaitOne() which will cause a deadlock (the processor.ProcessMessages() will go into an infinitive loop)

  • 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-17T06:42:27+00:00Added an answer on May 17, 2026 at 6:42 am

    Yes, the scenario can occur. Yes it can deadlock if you don’t declare the bool variable as volatile. Just don’t use a bool, use an event like you did.

    The logic looks weird, it smells like you are trying to let the main thread wait for the processing to be completed. The workDoneEvent doesn’t actually signal that the work was done. Right now the main thread will check the assert before the worker is done, that can’t be good. If the intention was that it signals that the worker is done then ProcessAndSignal should be the one calling Set(), at the end of the method. And the main thread should call WaitOne().

    If this is at all accurate then you just should not use QUWI, just call ProcessAndSignal directly without using a thread. Far more efficient, zero odds for threading problems.

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

Sidebar

Related Questions

Consider the following code snippet: class Test { public int Length{ get; set; }
Consider the following code snippet: list<someClass>& method(); .... list<someClass> test = method(); What will
Consider the following Code snippet, Assembly asm = Assembly.LoadFile(DLL_Path); Type t = asm.GetType(DLL_NameSpace.MyClass, false,
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following test case: public class Main { static int a = 0;
Consider the following test function: testError :: (Error e, MonadError e m) => Bool
Consider the following scenerio.... I have a master user MASTER. I have a test
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)

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.