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 8913777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:39+00:00 2026-06-15T04:32:39+00:00

In the below code I am trying to start two workers at the same

  • 0

In the below code I am trying to start two workers at the same time so that the over all elapsed time and the individual elapsed match each other. I was expecting over all elapsed time 1000 ms (+say 3 ms latency) and individual elapsed time of all the workers to be 1000ms.

But I got the output as if all the workers ran sequentially.

OverAll Elapsed Time: 2001
Individual Elapsed Time: 1000
Individual Elapsed Time: 999

Could you explain what am I missing?

class Program
{
    static int m_NumberOfWorkers = 2;
    static readonly object m_Locker = new object();
    static bool flag_GO = false;
    static Stopwatch m_OverAllStopwatch = new Stopwatch();
    static ConcurrentBag<Stopwatch> m_bagIndividualStopwatch = new ConcurrentBag<Stopwatch>();
    static int m_CompletedWorkers = 0;
    static void Main(string[] args)
    {   
        // Create Workers
        List<Thread> lstThreads = new List<Thread>();
        for (int i = 0; i < m_NumberOfWorkers; ++i)
        {
            lstThreads.Add(new Thread(ConcurrentMethod));
        }

        // Start Workers
        for (int i = 0; i < lstThreads.Count; ++i)
        {
            lstThreads[i].Start();
        }

        m_OverAllStopwatch.Start();

        // Signal all workers
        lock (m_Locker)
        {
            flag_GO = true;
            Monitor.PulseAll(m_Locker);
        }

        // Wait all workers to finish
        for (int i = 0; i < lstThreads.Count; ++i)
        {
            lstThreads[i].Join();
        }

        Console.Read();
    }

    private static void ConcurrentMethod()
    {
        lock (m_Locker)
        {
            while (!flag_GO) Monitor.Wait(m_Locker);
            TestWhatEverYouWant();
            IamDone();
        }
    }

    private static void TestWhatEverYouWant()
    {
        Stopwatch stopWatch = Stopwatch.StartNew();
        Thread.Sleep(1000);
        stopWatch.Stop();
        m_bagIndividualStopwatch.Add(stopWatch);
    }

    private static void IamDone()
    {
        Interlocked.Increment(ref m_CompletedWorkers);
        // Summarize results if all workers are done
        if (Interlocked.CompareExchange(ref m_CompletedWorkers, 0, m_NumberOfWorkers) == m_NumberOfWorkers)
        {
            m_OverAllStopwatch.Stop();
            Console.WriteLine("OverAll Elapsed Time: {0}", m_OverAllStopwatch.ElapsedMilliseconds);
            foreach (Stopwatch stopWatch in m_bagIndividualStopwatch)
            {
                Console.WriteLine("Individual Elapsed Time: {0}", stopWatch.ElapsedMilliseconds);
            }
        }
    }        
}
  • 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-06-15T04:32:40+00:00Added an answer on June 15, 2026 at 4:32 am

    Silly me 🙂 I was doing work with in a lock!!! Moving these methods out of the lock ofcourse solved the problem.

        private static void ConcurrentMethod()
        {
            lock (m_Locker)
            {
                while (!flag_GO) Monitor.Wait(m_Locker);                
            }
            TestWhatEverYouWant();
            IamDone();
        }
    

    And I got the desired output!!

    OverAll Elapsed Time: 1004
    Individual Elapsed Time: 1000
    Individual Elapsed Time: 999
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below code I'm trying to loop through each child node and append
I have two related qestions on the code included below 1) I am trying
I just now start using knockoutjs. In below code am just trying to bind
I cannot get Bootstrap Carousel to start, um, carousel-ing. Below is all the code
I have the following code below, I'm trying to compare two dates using Joda
I'm using the javascript code below trying to extract the number after gallery-entry_ in
In the code below i`m trying to read a list of selected projects and
I have been trying to access the below code DECLARE N_DEPTNO DEPT.DEPTNO %TYPE :=&DEPT_NUM;
In the below code, i am trying to reference an external .dll, which creates
I am trying to change desktop image using below code: NSError *anError = nil;

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.