Threads and ThreadPool's

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

    Threads and ThreadPool's

    Threads and ThreadPool's

    If I use a ThreadPool how can I tell when a thead in the threadpool has
    exited? I don't want to set a global member variable I would much rather be
    able to act on an event

    Also (failing this ThreadPool issue) is it possible to create an array of
    Worker Threads, can anyone illustrate with code please

    Thanks
    PT


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Threads and ThreadPool's

    Max,

    If you want to be notified when a thread in the ThreadPool exits, you
    would have to do it the same way that you would be notified when a thread
    exits. You would have to write code at the end of your method which would
    trigger some sort of notification event. This could be a delegate, an
    object you pass in to have a method called on it, etc, etc.

    If you wanted to create an array of worker threads, you can do it like
    creating an array of any other type, but in this case, I would advise
    against it. You would have to handle all the management yourself, and you
    are going to gain little from writing the code to determine which thread to
    use, keeping the thread alive, etc, etc.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Max Adams" <rubberducky703 @hotmail.com> wrote in message
    news:%23zgM6K6e EHA.636@TK2MSFT NGP12.phx.gbl.. .[color=blue]
    > Threads and ThreadPool's
    >
    > If I use a ThreadPool how can I tell when a thead in the threadpool has
    > exited? I don't want to set a global member variable I would much rather[/color]
    be[color=blue]
    > able to act on an event
    >
    > Also (failing this ThreadPool issue) is it possible to create an array of
    > Worker Threads, can anyone illustrate with code please
    >
    > Thanks
    > PT
    >
    >[/color]


    Comment

    • Richard

      #3
      RE: Threads and ThreadPool's


      I've done alot of Threaded code and I agree totally with Nicholas... Avoid
      writing your own ThreadPool stuff if possible; the .NET ThreadPool is playing
      games behind the scenes to be very efficient...

      As far as getting notification of completion it depends on your needs.

      If you can't sleep while the task runs {aka a GUI thread kicks off the task}
      then kick off the task and have the last step of the task be to call
      BeginInvoke on a an event that signals the GUI that the task has finished.

      If you just want to know when a single task has completed then queue the
      task and have the tasks last step be to set an event that wakes you up when
      it's finished.

      --Richard

      "Max Adams" wrote:
      [color=blue]
      > Threads and ThreadPool's
      >
      > If I use a ThreadPool how can I tell when a thead in the threadpool has
      > exited? I don't want to set a global member variable I would much rather be
      > able to act on an event
      >
      > Also (failing this ThreadPool issue) is it possible to create an array of
      > Worker Threads, can anyone illustrate with code please
      >
      > Thanks
      > PT
      >
      >
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        RE: Threads and ThreadPool's

        Richard <Richard@discus sions.microsoft .com> wrote:[color=blue]
        > I've done alot of Threaded code and I agree totally with Nicholas...
        > Avoid writing your own ThreadPool stuff if possible; the .NET
        > ThreadPool is playing games behind the scenes to be very efficient...[/color]

        On the other hand, it's completely out of your control and is used by
        other parts of the framework, which make it somewhat dangerous in some
        ways.

        Personally I wish the .NET framework had a flexible ThreadPool which
        could be created by an individual app and used exclusively by the app.

        As it is, I don't see much wrong with using your own ThreadPool,
        assuming you get a suitably robust implementation. You can then decide
        what it should do in terms of security contexts etc as well. Yes, the
        system thread pool has been written to be very efficient - but a lot of
        people don't need quite that level of efficiency. Better to work
        solidly at 95% of the optimum speed than run at 100% for a while and
        then accidentally fill up the system threadpool in a way which is
        unpredictable and deadlocks all the threads.

        --
        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

        • KSU Nair

          #5
          Re: Threads and ThreadPool's

          One of my suggestion on the thread pool is to make sure you don't
          create too many threads. Use the default 25 threads or lesser and
          write your own function to control the pooling so that you keep only
          required number of threads running all the time.
          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<#UUjCa7eE HA.1764@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
          > Max,
          >
          > If you want to be notified when a thread in the ThreadPool exits, you
          > would have to do it the same way that you would be notified when a thread
          > exits. You would have to write code at the end of your method which would
          > trigger some sort of notification event. This could be a delegate, an
          > object you pass in to have a method called on it, etc, etc.
          >
          > If you wanted to create an array of worker threads, you can do it like
          > creating an array of any other type, but in this case, I would advise
          > against it. You would have to handle all the management yourself, and you
          > are going to gain little from writing the code to determine which thread to
          > use, keeping the thread alive, etc, etc.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Max Adams" <rubberducky703 @hotmail.com> wrote in message
          > news:%23zgM6K6e EHA.636@TK2MSFT NGP12.phx.gbl.. .[color=green]
          > > Threads and ThreadPool's
          > >
          > > If I use a ThreadPool how can I tell when a thead in the threadpool has
          > > exited? I don't want to set a global member variable I would much rather[/color]
          > be[color=green]
          > > able to act on an event
          > >
          > > Also (failing this ThreadPool issue) is it possible to create an array of
          > > Worker Threads, can anyone illustrate with code please
          > >
          > > Thanks
          > > PT
          > >
          > >[/color][/color]

          Comment

          • Richard

            #6
            RE: Threads and ThreadPool's

            [color=blue]
            > On the other hand, it's completely out of your control and is used by
            > other parts of the framework, which make it somewhat dangerous in some
            > ways.
            >[/color]

            Agreed sort of: I don't like that I can't abort thread pool threads, and I
            don't like that I can't join on thread pool threads, etc... but it's not out
            of your control.

            For most cases I've seen the thread pool does very well. The big thing I
            see in people's code is that they tend to write overly complicated
            synchronization logic rather than learn how to use
            RegisterWaitFor SingleObject() properly...
            [color=blue]
            > Personally I wish the .NET framework had a flexible ThreadPool which
            > could be created by an individual app and used exclusively by the app.
            >[/color]

            For what I'm working on right now I have pondered creating just such a more
            flexible thread pool. A thread pool where I could Abort threads, set/change
            base priorities, num min & max threads, automatically synchronize batch
            start, and where I could WaitAll on an arbitrarily defined batch...
            [color=blue]
            > then accidentally fill up the system threadpool in a way which is
            > unpredictable and deadlocks all the threads.
            >[/color]

            Sounds like a bug in code to me - have you ever actually siezed up the pool,
            I've never seen that?

            --Richard

            Comment

            • Jon Skeet [C# MVP]

              #7
              RE: Threads and ThreadPool's

              Richard <Richard@discus sions.microsoft .com> wrote:[color=blue][color=green]
              > > On the other hand, it's completely out of your control and is used by
              > > other parts of the framework, which make it somewhat dangerous in some
              > > ways.[/color]
              >
              > Agreed sort of: I don't like that I can't abort thread pool threads,[/color]

              I don't have a problem with that, as I think aborting threads should
              always be a last resort which should only be used to abort the whole
              app. Aborting threads can leave the system in an ill-defined state,
              where you don't want to continue with the application anyway.
              [color=blue]
              > and I don't like that I can't join on thread pool threads, etc... but
              > it's not out of your control.[/color]

              It is - you can't dictate the thread creation or termination policy.
              You can't (easily) dictate the size. You don't get to easily control
              [color=blue]
              > For most cases I've seen the thread pool does very well. The big thing I
              > see in people's code is that they tend to write overly complicated
              > synchronization logic rather than learn how to use
              > RegisterWaitFor SingleObject() properly...
              >[color=green]
              > > Personally I wish the .NET framework had a flexible ThreadPool which
              > > could be created by an individual app and used exclusively by the app.[/color]
              >
              > For what I'm working on right now I have pondered creating just such a more
              > flexible thread pool. A thread pool where I could Abort threads, set/change
              > base priorities, num min & max threads, automatically synchronize batch
              > start, and where I could WaitAll on an arbitrarily defined batch...[/color]

              I've started work on a ThreadPool myself, although there's definitely
              more to add. It's available at


              tomThreadPool.c s

              If you have a look at it and spot any bugs, please let me know :)

              (There's definite refactoring needed in a few places - WorkerThreadLoo p
              is too big and complicated, for a start.)
              [color=blue][color=green]
              > > then accidentally fill up the system threadpool in a way which is
              > > unpredictable and deadlocks all the threads.[/color]
              >
              > Sounds like a bug in code to me - have you ever actually siezed up the pool,
              > I've never seen that?[/color]

              I haven't personally, although I've seen others complaining of it - and
              it doesn't have to be a bug. If you've got some threads in the
              threadpool waiting for other operations to complete, and those
              operations are waiting for a threadpool thread to execute on, you can
              end up with an effective deadlock. Don't forget that you don't always
              know which operations use the ThreadPool. For instance, I'm pretty sure
              that HttpWebRequest does. That's what I mean by the system ThreadPool
              being out of developers' 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

              Working...