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

The Archive Base Latest Questions

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadExample { public

  • 0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadExample
{
    public class Info
    {
        public int Counter;


        private static object _lock = new object();
        private List<Thread> ThreadList;

        public Info(int counter)
        {
            Counter = counter;

            ThreadList = new List<Thread>();

            ThreadList.Add(new Thread(ThreadBody));
            ThreadList.Add(new Thread(ThreadBody));

           ThreadList[0].Name = "t1";
           ThreadList[1].Name = "t2";

        }

        public void Start()
        {
            ThreadList.ForEach(t => t.Start(t.Name));
        }

        public void ThreadBody(object name)
        {
            while (Counter != 20)
            {
                lock (_lock)
                {
                    Counter++;

                    Console.WriteLine("Thread {0} : the value of the counter is {1}", name.ToString(), Counter);
                }
            }



        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThreadExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Info info = new Info(0);
            info.Start();
        }
    }
}

if the lock is just lock counter ++
lock (_lock)
{
Counter++;
}
I don’t have an infinite loop, but if the lock is like in example, it run infinite 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-22T16:36:45+00:00Added an answer on May 22, 2026 at 4:36 pm

    It could be that when Counter gets to 19 both threads enter the loop and it ends up getting incremented to 21 before they test the value again.

    You’ll need to hold the lock while reading the value of Counter. A double check of Counter might be adequate (re-read it again inside the while loop while holding the lock). However, I’m not sure about this because my head just can’t keep track of all the details of various threading memory models between native, .NET, Java, and whatever. Even on .NET the ECMA model is apparently different than what MS guarantees for their CLR (see http://msdn.microsoft.com/en-us/magazine/cc163715.aspx and http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx). For more details on why double-checking might or might not work, search for “double checked locking” – there’s an awful lot of complexity behind something that apparently should be simple.

    For example, here’s snippet of a run on my machine:

    Thread t1 : the value of the counter is 1
    Thread t2 : the value of the counter is 2
    Thread t2 : the value of the counter is 3
    Thread t2 : the value of the counter is 4
    Thread t2 : the value of the counter is 5
    Thread t2 : the value of the counter is 6
    Thread t2 : the value of the counter is 7
    Thread t2 : the value of the counter is 8
    Thread t2 : the value of the counter is 9
    Thread t2 : the value of the counter is 10
    Thread t2 : the value of the counter is 11
    Thread t2 : the value of the counter is 12
    Thread t2 : the value of the counter is 13
    Thread t2 : the value of the counter is 14
    Thread t2 : the value of the counter is 15
    Thread t2 : the value of the counter is 16
    Thread t2 : the value of the counter is 17
    Thread t2 : the value of the counter is 18
    Thread t2 : the value of the counter is 19
    Thread t2 : the value of the counter is 20
    Thread t1 : the value of the counter is 21
    Thread t1 : the value of the counter is 22
    
    ... Thread t1 never stops ...
    

    You’ll notice that t2 stops once it gets Counter to 20, but the t1 doesn’t notice that. It’s already entered the loop (or decided to enter the loop) thinking that Counter is 1 (or maybe 2 or something else – just not 20).

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

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadDemo { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program {
This is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Text.RegularExpressions; using System.Threading;
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Namespace error cant find which one? using System; using System.Collections.Generic; using System.Linq; using System.Text;

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.