Threading problem.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muscha

    #1

    Threading problem.

    Hi All,

    I'm having a bit of problem with threading, when I first call Start() on my
    code the thread work, I call Stop() and it stopped, but when I call Start()
    again my thread is not started. Although in the debugger I can see that it
    went to the Start() method and creating the thread again.

    Can anyone help me? This is the code:

    <snip>

    public static MySingleton GetInstance() {
    if (instance == null) {
    instance = new MySingleton();
    }
    return instance;
    }

    public void Start() {
    lock (this) {
    if (!started){
    started = true;
    thread = new Thread(new ThreadStart(Run ));
    thread.Start();
    }
    }
    }

    public void Stop() {
    stopped = true;
    }

    private void Run() {
    while (!stopped) {
    Console.WriteLi ne("Running ...");
    Thread.Sleep(10 00);
    }
    thread.Join();
    started = false;
    }

    </snip>


    Thanks,

    /m


  • Dan Cimpoiesu

    #2
    Re: Threading problem.

    Try thread.Abort() on the Stop method

    Hope this helps
    Dan Cimpoiesu

    "muscha" <muscha@no.spam .net> wrote in message
    news:ehox1DmhDH A.352@tk2msftng p13.phx.gbl...[color=blue]
    > Hi All,
    >
    > I'm having a bit of problem with threading, when I first call Start() on[/color]
    my[color=blue]
    > code the thread work, I call Stop() and it stopped, but when I call[/color]
    Start()[color=blue]
    > again my thread is not started. Although in the debugger I can see that it
    > went to the Start() method and creating the thread again.
    >
    > Can anyone help me? This is the code:
    >
    > <snip>
    >
    > public static MySingleton GetInstance() {
    > if (instance == null) {
    > instance = new MySingleton();
    > }
    > return instance;
    > }
    >
    > public void Start() {
    > lock (this) {
    > if (!started){
    > started = true;
    > thread = new Thread(new ThreadStart(Run ));
    > thread.Start();
    > }
    > }
    > }
    >
    > public void Stop() {
    > stopped = true;
    > }
    >
    > private void Run() {
    > while (!stopped) {
    > Console.WriteLi ne("Running ...");
    > Thread.Sleep(10 00);
    > }
    > thread.Join();
    > started = false;
    > }
    >
    > </snip>
    >
    >
    > Thanks,
    >
    > /m
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Threading problem.

      muscha <muscha@no.spam .net> wrote:[color=blue]
      > I'm having a bit of problem with threading, when I first call Start() on my
      > code the thread work, I call Stop() and it stopped, but when I call Start()
      > again my thread is not started. Although in the debugger I can see that it
      > went to the Start() method and creating the thread again.[/color]

      Your code is slightly odd - I don't see why you're trying to call
      thread.Join() within Run, when the thread it's running in should be the
      thread it's trying to join anyway!

      Note also that your singleton implementation isn't threadsafe - see
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      Finally, the way you're testing the "stopped" flag is dodgy - because
      you haven't got any synchronization there, there's no guarantee that
      other threads will see new values.

      If you could post a short but *complete* example which demonstrates the
      problem, that would help.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • José Joye

        #4
        Re: Threading problem.

        Change the 2 methods below and it should work:

        public void Stop()
        {
        lock (this)
        {
        stopped = true;
        thread.Join();
        started = false;
        stopped = false;
        }
        }

        private void Run()
        {
        while (!stopped)
        {
        Console.WriteLi ne("Running ...");
        Thread.Sleep(10 00);
        }
        }

        José


        "muscha" <muscha@no.spam .net> wrote in message
        news:ehox1DmhDH A.352@tk2msftng p13.phx.gbl...[color=blue]
        > Hi All,
        >
        > I'm having a bit of problem with threading, when I first call Start() on[/color]
        my[color=blue]
        > code the thread work, I call Stop() and it stopped, but when I call[/color]
        Start()[color=blue]
        > again my thread is not started. Although in the debugger I can see that it
        > went to the Start() method and creating the thread again.
        >
        > Can anyone help me? This is the code:
        >
        > <snip>
        >
        > public static MySingleton GetInstance() {
        > if (instance == null) {
        > instance = new MySingleton();
        > }
        > return instance;
        > }
        >
        > public void Start() {
        > lock (this) {
        > if (!started){
        > started = true;
        > thread = new Thread(new ThreadStart(Run ));
        > thread.Start();
        > }
        > }
        > }
        >
        > public void Stop() {
        > stopped = true;
        > }
        >
        > private void Run() {
        > while (!stopped) {
        > Console.WriteLi ne("Running ...");
        > Thread.Sleep(10 00);
        > }
        > thread.Join();
        > started = false;
        > }
        >
        > </snip>
        >
        >
        > Thanks,
        >
        > /m
        >
        >[/color]


        Comment

        • muscha

          #5
          Re: Threading problem.

          Jon and all,[color=blue]
          > Note also that your singleton implementation isn't threadsafe - see
          > http://www.pobox.com/~skeet/csharp/singleton.html[/color]

          Thanks for that.
          [color=blue]
          > If you could post a short but *complete* example which demonstrates the
          > problem, that would help.[/color]

          Here is my class, I followed people's suggestion and it seems working now,
          however is it thread safe?


          using System;
          using System.Threadin g;

          namespace Scheduler
          {
          public class MySingleton
          {
          private static MySingleton instance;
          private static Object padlock = new Object();
          private Thread thread;
          private bool stopped = false;
          private bool started = false;

          /// <summary>
          /// Private constructor so this thread can't be instantiated.
          /// </summary>
          private MySingleton()
          {
          }

          ~MySingleton()
          {
          Stop();
          }

          public static MySingletonGetI nstance()
          {
          if (instance == null)
          {
          lock (padlock)
          {
          if (instance == null)
          {
          instance = new MySingleton();
          }
          }
          }
          return instance;
          }

          public void Start()
          {
          lock (this)
          {
          if (!started)
          {
          started = true;
          thread = new Thread(new ThreadStart(thi s.Run));
          thread.Start();
          }
          }
          }

          public void Stop()
          {
          lock (this)
          {
          stopped = true;
          thread.Join();
          started = false;
          stopped = false;
          }
          }

          private void Run()
          {
          while (!stopped)
          {
          Console.WriteLi ne("Running ...");
          Thread.Sleep(10 00);
          }
          }
          }
          }

          Thanks,

          /m


          Comment

          • muscha

            #6
            Re: Threading problem.

            Jon and all,[color=blue]
            > Note also that your singleton implementation isn't threadsafe - see
            > http://www.pobox.com/~skeet/csharp/singleton.html[/color]

            Thanks for that.
            [color=blue]
            > If you could post a short but *complete* example which demonstrates the
            > problem, that would help.[/color]

            Here is my class, I followed people's suggestion and it seems working now,
            however is it thread safe?


            using System;
            using System.Threadin g;

            namespace Scheduler
            {
            public class MySingleton
            {
            private static MySingleton instance;
            private static Object padlock = new Object();
            private Thread thread;
            private bool stopped = false;
            private bool started = false;

            /// <summary>
            /// Private constructor so this thread can't be instantiated.
            /// </summary>
            private MySingleton()
            {
            }

            ~MySingleton()
            {
            Stop();
            }

            public static MySingletonGetI nstance()
            {
            if (instance == null)
            {
            lock (padlock)
            {
            if (instance == null)
            {
            instance = new MySingleton();
            }
            }
            }
            return instance;
            }

            public void Start()
            {
            lock (this)
            {
            if (!started)
            {
            started = true;
            thread = new Thread(new ThreadStart(thi s.Run));
            thread.Start();
            }
            }
            }

            public void Stop()
            {
            lock (this)
            {
            stopped = true;
            thread.Join();
            started = false;
            stopped = false;
            }
            }

            private void Run()
            {
            while (!stopped)
            {
            Console.WriteLi ne("Running ...");
            Thread.Sleep(10 00);
            }
            }
            }
            }

            Thanks,

            /m


            Comment

            • Doug Forster

              #7
              Re: Threading problem.

              Hi Muscha,

              Looks to be thread safe after a quick glance anyway. It it were my code I
              would first check that the thread is running in Stop to guard against the
              possibility that Start was never called.

              Cheers

              Doug Forster

              "muscha" <muscha@no.spam .net> wrote in message
              news:uofhlRvhDH A.944@TK2MSFTNG P11.phx.gbl...[color=blue]
              > Jon and all,[color=green]
              > > Note also that your singleton implementation isn't threadsafe - see
              > > http://www.pobox.com/~skeet/csharp/singleton.html[/color]
              >
              > Thanks for that.
              >[color=green]
              > > If you could post a short but *complete* example which demonstrates the
              > > problem, that would help.[/color]
              >
              > Here is my class, I followed people's suggestion and it seems working now,
              > however is it thread safe?
              >
              >
              > using System;
              > using System.Threadin g;
              >
              > namespace Scheduler
              > {
              > public class MySingleton
              > {
              > private static MySingleton instance;
              > private static Object padlock = new Object();
              > private Thread thread;
              > private bool stopped = false;
              > private bool started = false;
              >
              > /// <summary>
              > /// Private constructor so this thread can't be instantiated.
              > /// </summary>
              > private MySingleton()
              > {
              > }
              >
              > ~MySingleton()
              > {
              > Stop();
              > }
              >
              > public static MySingletonGetI nstance()
              > {
              > if (instance == null)
              > {
              > lock (padlock)
              > {
              > if (instance == null)
              > {
              > instance = new MySingleton();
              > }
              > }
              > }
              > return instance;
              > }
              >
              > public void Start()
              > {
              > lock (this)
              > {
              > if (!started)
              > {
              > started = true;
              > thread = new Thread(new ThreadStart(thi s.Run));
              > thread.Start();
              > }
              > }
              > }
              >
              > public void Stop()
              > {
              > lock (this)
              > {
              > stopped = true;
              > thread.Join();
              > started = false;
              > stopped = false;
              > }
              > }
              >
              > private void Run()
              > {
              > while (!stopped)
              > {
              > Console.WriteLi ne("Running ...");
              > Thread.Sleep(10 00);
              > }
              > }
              > }
              > }
              >
              > Thanks,
              >
              > /m
              >
              >[/color]


              Comment

              • Jon Skeet

                #8
                Re: Threading problem.

                muscha <muscha@no.spam .net> wrote:[color=blue]
                > Jon and all,[color=green]
                > > Note also that your singleton implementation isn't threadsafe - see
                > > http://www.pobox.com/~skeet/csharp/singleton.html[/color]
                >
                > Thanks for that.[/color]

                I note that you're using the double-checked locking algorithm though,
                which I don't believe to be thread-safe.
                [color=blue][color=green]
                > > If you could post a short but *complete* example which demonstrates the
                > > problem, that would help.[/color]
                >
                > Here is my class, I followed people's suggestion and it seems working now,
                > however is it thread safe?[/color]

                Not quite. Aside from the singleton problem, you're not locking on
                anything before checking the stopped flag in Run. I would also suggest
                not locking on "this" but locking on another reference private to the
                class (just create a new object, like padlock but with an instance
                variable).

                I'm also not keen on the finalizer - do you think you really need it,
                particularly? It's only going to be invoked when the AppDomain is
                unloaded, and then only "some time after that". I would personaly just
                get rid of it.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • muscha

                  #9
                  Re: Threading problem.

                  > > Here is my class, I followed people's suggestion and it seems working
                  now,[color=blue][color=green]
                  > > however is it thread safe?[/color]
                  >
                  > Not quite. Aside from the singleton problem, you're not locking on
                  > anything before checking the stopped flag in Run. I would also suggest
                  > not locking on "this" but locking on another reference private to the
                  > class (just create a new object, like padlock but with an instance
                  > variable).[/color]

                  What's the difference from locking a separate object to lock than doing lock
                  on this? Isn't that by doing lock(this) in the beginning of the method, you
                  practically synchronize the entire method call?

                  thanks,

                  /m




                  Comment

                  • Jon Skeet

                    #10
                    Re: Threading problem.

                    muscha <muscha@no.spam .net> wrote:[color=blue][color=green]
                    > > Not quite. Aside from the singleton problem, you're not locking on
                    > > anything before checking the stopped flag in Run. I would also suggest
                    > > not locking on "this" but locking on another reference private to the
                    > > class (just create a new object, like padlock but with an instance
                    > > variable).[/color]
                    >
                    > What's the difference from locking a separate object to lock than doing lock
                    > on this? Isn't that by doing lock(this) in the beginning of the method, you
                    > practically synchronize the entire method call?[/color]

                    If you lock on this, then something else might also lock on it, and you
                    could end up with a deadlock. If you lock on a reference that only your
                    instance will ever know about, you have more control.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Jon Skeet

                      #11
                      Re: Threading problem.

                      "Doug Forster" <doug_ZAPTHIS_A T_TONIQ_ZAPTHIS _co.nz> wrote:[color=blue]
                      > Looks to be thread safe after a quick glance anyway.[/color]

                      And that's why you should never trust multithreading code to quick
                      glances :)
                      [color=blue]
                      > It it were my code I
                      > would first check that the thread is running in Stop to guard against the
                      > possibility that Start was never called.[/color]

                      That would indeed be a good thing to do - I missed that.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...