Starting a new Thread vs. ThreadPool

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

    Starting a new Thread vs. ThreadPool

    Hello,

    I have always used a certain design pattern for multithreaded Windows app;
    Start new worker thread from UI thread, use events to notify UI threads when
    something happens, update UI controls using delegates through .BeginInvoke. I
    came across some code samples recently where ThreadPool class is used to
    start new threads.
    My questions;
    1. what's the difference?
    2. Any performance benefits?
    3. Can ThreadPool be used for the above-mentioned design pattern?

    If you could provide some insights on ThreadPool and specific instances when
    ThreadPool should (or must) be used, I would greatly appreciate it.
    Thank you
  • Helge Jensen

    #2
    Re: Starting a new Thread vs. ThreadPool



    Lenn wrote:[color=blue]
    > Hello,
    >
    > I have always used a certain design pattern for multithreaded Windows app;
    > Start new worker thread from UI thread, use events to notify UI threads when
    > something happens, update UI controls using delegates through .BeginInvoke. I
    > came across some code samples recently where ThreadPool class is used to
    > start new threads.[/color]
    [color=blue]
    > My questions;
    > 1. what's the difference?[/color]

    There are only 25 threads in the threadpool, code queued to execute must
    await the termination of a running thread in the pool.

    This means that you could get into a starvation situation where none of
    the threads in the pool terminate because the threads that updates the
    state so they can continue cannot run :)

    If you had started a thread instead you might have 1000s of them
    running, and you get an error when you try to *start* the thread, not a
    deadlock.
    [color=blue]
    > 2. Any performance benefits?[/color]

    Try a profiler, post it here if you find any difference worth
    mentioning. Remember that (hopefully) less that 1% of your code is
    spawning threads, so you only get very little of any bechmarked improvement.
    [color=blue]
    > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]

    You are *already* using the threadpool in the exact way it's meant to be
    used: to run short pieces of code that will soon terminate. .BeginInvoke
    queues execution on the threadpool.
    [color=blue]
    > If you could provide some insights on ThreadPool and specific instances when
    > ThreadPool should (or must) be used, I would greatly appreciate it.[/color]

    There is no code for which it should or must be used, let the compiler
    use it for .BeginInvoke.

    Do not use it for things that you expect to block, waiting for other
    threads.

    --
    Helge Jensen
    mailto:helge.je nsen@slog.dk
    sip:helge.jense n@slog.dk
    -=> Sebastian cover-music: http://ungdomshus.nu <=-

    Comment

    • Helge Jensen

      #3
      Re: Starting a new Thread vs. ThreadPool



      Lenn wrote:[color=blue]
      > Hello,
      >
      > I have always used a certain design pattern for multithreaded Windows app;
      > Start new worker thread from UI thread, use events to notify UI threads when
      > something happens, update UI controls using delegates through .BeginInvoke. I
      > came across some code samples recently where ThreadPool class is used to
      > start new threads.[/color]
      [color=blue]
      > My questions;
      > 1. what's the difference?[/color]

      There are only 25 threads in the threadpool, code queued to execute must
      await the termination of a running thread in the pool.

      This means that you could get into a starvation situation where none of
      the threads in the pool terminate because the threads that updates the
      state so they can continue cannot run :)

      If you had started a thread instead you might have 1000s of them
      running, and you get an error when you try to *start* the thread, not a
      deadlock.
      [color=blue]
      > 2. Any performance benefits?[/color]

      Try a profiler, post it here if you find any difference worth
      mentioning. Remember that (hopefully) less that 1% of your code is
      spawning threads, so you only get very little of any bechmarked improvement.
      [color=blue]
      > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]

      You are *already* using the threadpool in the exact way it's meant to be
      used: to run short pieces of code that will soon terminate. .BeginInvoke
      queues execution on the threadpool.
      [color=blue]
      > If you could provide some insights on ThreadPool and specific instances when
      > ThreadPool should (or must) be used, I would greatly appreciate it.[/color]

      There is no code for which it should or must be used, let the compiler
      use it for .BeginInvoke.

      Do not use it for things that you expect to block, waiting for other
      threads.

      --
      Helge Jensen
      mailto:helge.je nsen@slog.dk
      sip:helge.jense n@slog.dk
      -=> Sebastian cover-music: http://ungdomshus.nu <=-

      Comment

      • Brian Gideon

        #4
        Re: Starting a new Thread vs. ThreadPool


        Lenn wrote:[color=blue]
        > Hello,
        >
        > I have always used a certain design pattern for multithreaded Windows app;
        > Start new worker thread from UI thread, use events to notify UI threads when
        > something happens, update UI controls using delegates through .BeginInvoke. I
        > came across some code samples recently where ThreadPool class is used to
        > start new threads.
        > My questions;[/color]
        [color=blue]
        > 1. what's the difference?[/color]

        The ThreadPool is managed by the framework. It is a fixed size pool of
        threads that the framework uses to run work items asynchronously. It's
        very easy to use. You simply make a call to
        ThreadPool.Queu eUserWorkItem and pass in a delegate of the method you
        want to run.
        [color=blue]
        > 2. Any performance benefits?[/color]

        Yes, especially for work items that execute quickly. Since the threads
        have already been created there is little overhead in getting a work
        item to execute.
        [color=blue]
        > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]

        It depends. If the task you're wanting to run asynchronously takes a
        long time to complete then no. You're better off managing your own
        worker thread. If the task completes very quickly then it might be a
        candidate. If you could explain what your existing worker thread does
        then we could offer better advice.
        [color=blue]
        > If you could provide some insights on ThreadPool and specific instances when
        > ThreadPool should (or must) be used, I would greatly appreciate it.
        > Thank you[/color]

        Like I said, generally tasks that complete quickly are candidates.

        Comment

        • Brian Gideon

          #5
          Re: Starting a new Thread vs. ThreadPool


          Lenn wrote:[color=blue]
          > Hello,
          >
          > I have always used a certain design pattern for multithreaded Windows app;
          > Start new worker thread from UI thread, use events to notify UI threads when
          > something happens, update UI controls using delegates through .BeginInvoke. I
          > came across some code samples recently where ThreadPool class is used to
          > start new threads.
          > My questions;[/color]
          [color=blue]
          > 1. what's the difference?[/color]

          The ThreadPool is managed by the framework. It is a fixed size pool of
          threads that the framework uses to run work items asynchronously. It's
          very easy to use. You simply make a call to
          ThreadPool.Queu eUserWorkItem and pass in a delegate of the method you
          want to run.
          [color=blue]
          > 2. Any performance benefits?[/color]

          Yes, especially for work items that execute quickly. Since the threads
          have already been created there is little overhead in getting a work
          item to execute.
          [color=blue]
          > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]

          It depends. If the task you're wanting to run asynchronously takes a
          long time to complete then no. You're better off managing your own
          worker thread. If the task completes very quickly then it might be a
          candidate. If you could explain what your existing worker thread does
          then we could offer better advice.
          [color=blue]
          > If you could provide some insights on ThreadPool and specific instances when
          > ThreadPool should (or must) be used, I would greatly appreciate it.
          > Thank you[/color]

          Like I said, generally tasks that complete quickly are candidates.

          Comment

          • Brian Gideon

            #6
            Re: Starting a new Thread vs. ThreadPool

            Helge Jensen wrote:[color=blue]
            > Lenn wrote:[color=green]
            > > Hello,
            > >
            > > I have always used a certain design pattern for multithreaded Windows app;
            > > Start new worker thread from UI thread, use events to notify UI threads when
            > > something happens, update UI controls using delegates through .BeginInvoke. I
            > > came across some code samples recently where ThreadPool class is used to
            > > start new threads.[/color]
            >[color=green]
            > > My questions;
            > > 1. what's the difference?[/color]
            >
            > There are only 25 threads in the threadpool, code queued to execute must
            > await the termination of a running thread in the pool.
            >
            > This means that you could get into a starvation situation where none of
            > the threads in the pool terminate because the threads that updates the
            > state so they can continue cannot run :)
            >
            > If you had started a thread instead you might have 1000s of them
            > running, and you get an error when you try to *start* the thread, not a
            > deadlock.
            >[color=green]
            > > 2. Any performance benefits?[/color]
            >
            > Try a profiler, post it here if you find any difference worth
            > mentioning. Remember that (hopefully) less that 1% of your code is
            > spawning threads, so you only get very little of any bechmarked improvement.
            >[color=green]
            > > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]
            >
            > You are *already* using the threadpool in the exact way it's meant to be
            > used: to run short pieces of code that will soon terminate. .BeginInvoke
            > queues execution on the threadpool.
            >[/color]

            The OP is not using the ThreadPool currently. The BeginInvoke method
            the OP is speaking of is actually ISynchronizeInv oke.BeginInvoke . When
            used on a Form or Control this method marshals the execution a of
            delegate onto the thread hosting that form or control (the UI thread).
            In this case execution is queued and executed by the message loop.
            [color=blue][color=green]
            > > If you could provide some insights on ThreadPool and specific instances when
            > > ThreadPool should (or must) be used, I would greatly appreciate it.[/color]
            >
            > There is no code for which it should or must be used, let the compiler
            > use it for .BeginInvoke.
            >
            > Do not use it for things that you expect to block, waiting for other
            > threads.
            >
            > --
            > Helge Jensen
            > mailto:helge.je nsen@slog.dk
            > sip:helge.jense n@slog.dk
            > -=> Sebastian cover-music: http://ungdomshus.nu <=-[/color]

            Comment

            • Brian Gideon

              #7
              Re: Starting a new Thread vs. ThreadPool

              Helge Jensen wrote:[color=blue]
              > Lenn wrote:[color=green]
              > > Hello,
              > >
              > > I have always used a certain design pattern for multithreaded Windows app;
              > > Start new worker thread from UI thread, use events to notify UI threads when
              > > something happens, update UI controls using delegates through .BeginInvoke. I
              > > came across some code samples recently where ThreadPool class is used to
              > > start new threads.[/color]
              >[color=green]
              > > My questions;
              > > 1. what's the difference?[/color]
              >
              > There are only 25 threads in the threadpool, code queued to execute must
              > await the termination of a running thread in the pool.
              >
              > This means that you could get into a starvation situation where none of
              > the threads in the pool terminate because the threads that updates the
              > state so they can continue cannot run :)
              >
              > If you had started a thread instead you might have 1000s of them
              > running, and you get an error when you try to *start* the thread, not a
              > deadlock.
              >[color=green]
              > > 2. Any performance benefits?[/color]
              >
              > Try a profiler, post it here if you find any difference worth
              > mentioning. Remember that (hopefully) less that 1% of your code is
              > spawning threads, so you only get very little of any bechmarked improvement.
              >[color=green]
              > > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]
              >
              > You are *already* using the threadpool in the exact way it's meant to be
              > used: to run short pieces of code that will soon terminate. .BeginInvoke
              > queues execution on the threadpool.
              >[/color]

              The OP is not using the ThreadPool currently. The BeginInvoke method
              the OP is speaking of is actually ISynchronizeInv oke.BeginInvoke . When
              used on a Form or Control this method marshals the execution a of
              delegate onto the thread hosting that form or control (the UI thread).
              In this case execution is queued and executed by the message loop.
              [color=blue][color=green]
              > > If you could provide some insights on ThreadPool and specific instances when
              > > ThreadPool should (or must) be used, I would greatly appreciate it.[/color]
              >
              > There is no code for which it should or must be used, let the compiler
              > use it for .BeginInvoke.
              >
              > Do not use it for things that you expect to block, waiting for other
              > threads.
              >
              > --
              > Helge Jensen
              > mailto:helge.je nsen@slog.dk
              > sip:helge.jense n@slog.dk
              > -=> Sebastian cover-music: http://ungdomshus.nu <=-[/color]

              Comment

              • Helge Jensen

                #8
                Re: Starting a new Thread vs. ThreadPool



                Brian Gideon wrote:[color=blue]
                > Helge Jensen wrote:[/color]
                [color=blue]
                > The OP is not using the ThreadPool currently. The BeginInvoke method
                > the OP is speaking of is actually ISynchronizeInv oke.BeginInvoke .[/color]

                I see.

                --
                Helge Jensen
                mailto:helge.je nsen@slog.dk
                sip:helge.jense n@slog.dk
                -=> Sebastian cover-music: http://ungdomshus.nu <=-

                Comment

                • Helge Jensen

                  #9
                  Re: Starting a new Thread vs. ThreadPool



                  Brian Gideon wrote:[color=blue]
                  > Helge Jensen wrote:[/color]
                  [color=blue]
                  > The OP is not using the ThreadPool currently. The BeginInvoke method
                  > the OP is speaking of is actually ISynchronizeInv oke.BeginInvoke .[/color]

                  I see.

                  --
                  Helge Jensen
                  mailto:helge.je nsen@slog.dk
                  sip:helge.jense n@slog.dk
                  -=> Sebastian cover-music: http://ungdomshus.nu <=-

                  Comment

                  • Lenn

                    #10
                    Re: Starting a new Thread vs. ThreadPool

                    Thank you all,

                    From what I am hearing ThreadPool will not make my life easier in any form.
                    So far, I developed a few .NET applications, that automate certain production
                    tasks. Mostly has to do with pulling data from SQL Server and/or legacy file
                    server, processing it, and generating reports. So, I usually start a worker
                    thread that in turn makes calls to different objects that do all those
                    smaller tasks, worker thread raises events when certain tasks complete or
                    fail. Worker thread itself might spun a few new threads for processing IO
                    Streams.
                    Depending on how much data it has to deal with, it can run from 4sec to
                    couple of hours. Of course nice UI with progress bars keeps user updated with
                    what's going on.
                    So far, it worked great for the user, I thought maybe I could put each
                    smaller tasks in ThreadPool, but now I don't think it would make that much of
                    a difference.


                    "Brian Gideon" wrote:
                    [color=blue]
                    >
                    > Lenn wrote:[color=green]
                    > > Hello,
                    > >
                    > > I have always used a certain design pattern for multithreaded Windows app;
                    > > Start new worker thread from UI thread, use events to notify UI threads when
                    > > something happens, update UI controls using delegates through .BeginInvoke. I
                    > > came across some code samples recently where ThreadPool class is used to
                    > > start new threads.
                    > > My questions;[/color]
                    >[color=green]
                    > > 1. what's the difference?[/color]
                    >
                    > The ThreadPool is managed by the framework. It is a fixed size pool of
                    > threads that the framework uses to run work items asynchronously. It's
                    > very easy to use. You simply make a call to
                    > ThreadPool.Queu eUserWorkItem and pass in a delegate of the method you
                    > want to run.
                    >[color=green]
                    > > 2. Any performance benefits?[/color]
                    >
                    > Yes, especially for work items that execute quickly. Since the threads
                    > have already been created there is little overhead in getting a work
                    > item to execute.
                    >[color=green]
                    > > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]
                    >
                    > It depends. If the task you're wanting to run asynchronously takes a
                    > long time to complete then no. You're better off managing your own
                    > worker thread. If the task completes very quickly then it might be a
                    > candidate. If you could explain what your existing worker thread does
                    > then we could offer better advice.
                    >[color=green]
                    > > If you could provide some insights on ThreadPool and specific instances when
                    > > ThreadPool should (or must) be used, I would greatly appreciate it.
                    > > Thank you[/color]
                    >
                    > Like I said, generally tasks that complete quickly are candidates.
                    >
                    >[/color]

                    Comment

                    • Lenn

                      #11
                      Re: Starting a new Thread vs. ThreadPool

                      Thank you all,

                      From what I am hearing ThreadPool will not make my life easier in any form.
                      So far, I developed a few .NET applications, that automate certain production
                      tasks. Mostly has to do with pulling data from SQL Server and/or legacy file
                      server, processing it, and generating reports. So, I usually start a worker
                      thread that in turn makes calls to different objects that do all those
                      smaller tasks, worker thread raises events when certain tasks complete or
                      fail. Worker thread itself might spun a few new threads for processing IO
                      Streams.
                      Depending on how much data it has to deal with, it can run from 4sec to
                      couple of hours. Of course nice UI with progress bars keeps user updated with
                      what's going on.
                      So far, it worked great for the user, I thought maybe I could put each
                      smaller tasks in ThreadPool, but now I don't think it would make that much of
                      a difference.


                      "Brian Gideon" wrote:
                      [color=blue]
                      >
                      > Lenn wrote:[color=green]
                      > > Hello,
                      > >
                      > > I have always used a certain design pattern for multithreaded Windows app;
                      > > Start new worker thread from UI thread, use events to notify UI threads when
                      > > something happens, update UI controls using delegates through .BeginInvoke. I
                      > > came across some code samples recently where ThreadPool class is used to
                      > > start new threads.
                      > > My questions;[/color]
                      >[color=green]
                      > > 1. what's the difference?[/color]
                      >
                      > The ThreadPool is managed by the framework. It is a fixed size pool of
                      > threads that the framework uses to run work items asynchronously. It's
                      > very easy to use. You simply make a call to
                      > ThreadPool.Queu eUserWorkItem and pass in a delegate of the method you
                      > want to run.
                      >[color=green]
                      > > 2. Any performance benefits?[/color]
                      >
                      > Yes, especially for work items that execute quickly. Since the threads
                      > have already been created there is little overhead in getting a work
                      > item to execute.
                      >[color=green]
                      > > 3. Can ThreadPool be used for the above-mentioned design pattern?[/color]
                      >
                      > It depends. If the task you're wanting to run asynchronously takes a
                      > long time to complete then no. You're better off managing your own
                      > worker thread. If the task completes very quickly then it might be a
                      > candidate. If you could explain what your existing worker thread does
                      > then we could offer better advice.
                      >[color=green]
                      > > If you could provide some insights on ThreadPool and specific instances when
                      > > ThreadPool should (or must) be used, I would greatly appreciate it.
                      > > Thank you[/color]
                      >
                      > Like I said, generally tasks that complete quickly are candidates.
                      >
                      >[/color]

                      Comment

                      Working...