ThreadPool wait for threads to finish

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

    ThreadPool wait for threads to finish

    Hi there

    I am looking at using a thread-pool, for example one written by Jon Skeet
    (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this
    pool provides the possibility to wait for all its threads to finish?

    For example, if I start 20 threads:

    CustomThreadPoo l pool = new CustomThreadPoo l("PetersThread Pool");
    ThreadMethod m = new ThreadMethod(In sertThread);
    for (int i = 0; i < 20; i++)
    {
    pool.AddWorkIte m(m, i);
    }

    [
    where my thread method is:
    delegate object ThreadMethod(in t val);
    private object InsertThread(in t val)
    {
    Console.WriteLi ne("Thread " + val);
    return 0;
    }
    ]

    How do I wait until all the threads are finished, before my program should
    continue? Do I do this here:
    while (pool.WorkingTh reads > 0)
    {
    }

    Thanks,
    Peter


  • Shardool Karnik

    #2
    Re: ThreadPool wait for threads to finish

    just a thought ...

    have each thread report to the main thread upon completion and then check
    the thread count at that point to see if others are still running....inst ead
    of a while loop


    "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
    news:ejKcWl7eFH A.2700@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi there
    >
    > I am looking at using a thread-pool, for example one written by Jon Skeet
    > (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if[/color]
    this[color=blue]
    > pool provides the possibility to wait for all its threads to finish?
    >
    > For example, if I start 20 threads:
    >
    > CustomThreadPoo l pool = new CustomThreadPoo l("PetersThread Pool");
    > ThreadMethod m = new ThreadMethod(In sertThread);
    > for (int i = 0; i < 20; i++)
    > {
    > pool.AddWorkIte m(m, i);
    > }
    >
    > [
    > where my thread method is:
    > delegate object ThreadMethod(in t val);
    > private object InsertThread(in t val)
    > {
    > Console.WriteLi ne("Thread " + val);
    > return 0;
    > }
    > ]
    >
    > How do I wait until all the threads are finished, before my program should
    > continue? Do I do this here:
    > while (pool.WorkingTh reads > 0)
    > {
    > }
    >
    > Thanks,
    > Peter
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: ThreadPool wait for threads to finish

      Shardool Karnik <TheOutlander@g mail.com> wrote:[color=blue]
      > have each thread report to the main thread upon completion and then check
      > the thread count at that point to see if others are still running....inst ead
      > of a while loop[/color]

      That would work - or make each thread "produce" on a semaphore, and
      wait (from the main thread) until the semaphore had been hit the
      appropriate number of times.

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

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: ThreadPool wait for threads to finish

        Hi,

        If you want to execute them in sequence what you need to make a thread for?

        what if you do this:
        1- Create a queue with the correct delegates referencing the methods you
        want to execute
        2- Create a thread and start Pop-ing the delegates and executing in turn.

        The only drawback may be that you cannot stop an individual item, you could
        finish the thread but it would finish the entire execution, but you may find
        other approach to execute the rest.


        cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
        news:ejKcWl7eFH A.2700@tk2msftn gp13.phx.gbl...[color=blue]
        > Hi there
        >
        > I am looking at using a thread-pool, for example one written by Jon Skeet
        > (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
        > this pool provides the possibility to wait for all its threads to finish?
        >
        > For example, if I start 20 threads:
        >
        > CustomThreadPoo l pool = new CustomThreadPoo l("PetersThread Pool");
        > ThreadMethod m = new ThreadMethod(In sertThread);
        > for (int i = 0; i < 20; i++)
        > {
        > pool.AddWorkIte m(m, i);
        > }
        >
        > [
        > where my thread method is:
        > delegate object ThreadMethod(in t val);
        > private object InsertThread(in t val)
        > {
        > Console.WriteLi ne("Thread " + val);
        > return 0;
        > }
        > ]
        >
        > How do I wait until all the threads are finished, before my program should
        > continue? Do I do this here:
        > while (pool.WorkingTh reads > 0)
        > {
        > }
        >
        > Thanks,
        > Peter
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: ThreadPool wait for threads to finish

          <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
          dot.state.fl.us >> wrote:[color=blue]
          > If you want to execute them in sequence what you need to make a thread for?[/color]

          He didn't say he wanted to execute them in sequence - he said he wanted
          to wait until they were all done. That's perfectly reasonable. For
          instance, you might be writing a web app which has to query several
          different data sources, and you want to do that concurrently, but you
          can't actually complete the request until all the separate queries are
          complete.

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

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: ThreadPool wait for threads to finish

            Hi,

            You are right, I have no idea where I get that idea from :)

            cheers,

            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1d2b9e 4ddda0de8598c3c f@msnews.micros oft.com...[color=blue]
            > <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
            > dot.state.fl.us >> wrote:[color=green]
            >> If you want to execute them in sequence what you need to make a thread
            >> for?[/color]
            >
            > He didn't say he wanted to execute them in sequence - he said he wanted
            > to wait until they were all done. That's perfectly reasonable. For
            > instance, you might be writing a web app which has to query several
            > different data sources, and you want to do that concurrently, but you
            > can't actually complete the request until all the separate queries are
            > complete.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Howard Swope

              #7
              Re: ThreadPool wait for threads to finish

              I needed to do the same thing and came up with the following solution. I
              don't know how effective it is in all cases, but it worked in my situation.
              private static AutoResetEvent AllThreadsDone = new
              AutoResetEvent( false);

              public static bool WaitForThreadPo olIdle(int timeout)

              {

              bool ret = false;



              Thread threadMonitor = new Thread(new
              ThreadStart(Mon itorThreads));

              threadMonitor.S tart();



              ret = AllThreadsDone. WaitOne(timeout ,false);

              if (!ret)

              {

              threadMonitor.A bort();

              }

              return ret;

              }



              private static void MonitorThreads( )

              {

              int mwt, miot, awt, aiot;

              ThreadPool.GetM axThreads(out mwt, out miot);



              // make sure all the thread pool threads are done working

              awt = aiot = 0;

              while(mwt != awt || miot != aiot)

              {

              ThreadPool.GetA vailableThreads (out awt, out aiot);

              Thread.Sleep(0) ;

              }

              Thread.Sleep(0) ;

              AllThreadsDone. Set();

              }



              "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
              news:ejKcWl7eFH A.2700@tk2msftn gp13.phx.gbl...[color=blue]
              > Hi there
              >
              > I am looking at using a thread-pool, for example one written by Jon Skeet
              > (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
              > this pool provides the possibility to wait for all its threads to finish?
              >
              > For example, if I start 20 threads:
              >
              > CustomThreadPoo l pool = new CustomThreadPoo l("PetersThread Pool");
              > ThreadMethod m = new ThreadMethod(In sertThread);
              > for (int i = 0; i < 20; i++)
              > {
              > pool.AddWorkIte m(m, i);
              > }
              >
              > [
              > where my thread method is:
              > delegate object ThreadMethod(in t val);
              > private object InsertThread(in t val)
              > {
              > Console.WriteLi ne("Thread " + val);
              > return 0;
              > }
              > ]
              >
              > How do I wait until all the threads are finished, before my program should
              > continue? Do I do this here:
              > while (pool.WorkingTh reads > 0)
              > {
              > }
              >
              > Thanks,
              > Peter
              >[/color]


              Comment

              • James Swindell

                #8
                Re: ThreadPool wait for threads to finish

                Peter,

                Are you opposed to creating a group of reset events?


                "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
                news:ejKcWl7eFH A.2700@tk2msftn gp13.phx.gbl...[color=blue]
                > Hi there
                >
                > I am looking at using a thread-pool, for example one written by Jon Skeet
                > (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
                > this pool provides the possibility to wait for all its threads to finish?
                >
                > For example, if I start 20 threads:
                >
                > CustomThreadPoo l pool = new CustomThreadPoo l("PetersThread Pool");
                > ThreadMethod m = new ThreadMethod(In sertThread);
                > for (int i = 0; i < 20; i++)
                > {
                > pool.AddWorkIte m(m, i);
                > }
                >
                > [
                > where my thread method is:
                > delegate object ThreadMethod(in t val);
                > private object InsertThread(in t val)
                > {
                > Console.WriteLi ne("Thread " + val);
                > return 0;
                > }
                > ]
                >
                > How do I wait until all the threads are finished, before my program should
                > continue? Do I do this here:
                > while (pool.WorkingTh reads > 0)
                > {
                > }
                >
                > Thanks,
                > Peter
                >[/color]


                Comment

                • Peter Kirk

                  #9
                  Re: ThreadPool wait for threads to finish


                  "James Swindell" <jamlogic@hotma il.com> skrev i en meddelelse
                  news:OSz9iOLfFH A.3316@TK2MSFTN GP14.phx.gbl...[color=blue]
                  > Peter,
                  >
                  > Are you opposed to creating a group of reset events?[/color]

                  No, I am not opposed to that if it would get the job done, but I have now
                  found a solution where I use a "counting semaphore". When I create a thread
                  I increment the semphore count, and each thread gets a reference to the
                  semaphore which it signals (decrements) when done, and in my main thread I
                  wait for the semaphore count to reach 0 again.

                  Thanks,
                  Peter


                  Comment

                  • Peter Kirk

                    #10
                    Re: ThreadPool wait for threads to finish

                    "Howard Swope" <howardsnewsATs pitzincDOTcom> skrev i en meddelelse
                    news:u5fzh3KfFH A.352@TK2MSFTNG P09.phx.gbl...[color=blue]
                    >I needed to do the same thing and came up with the following solution. I
                    >don't know how effective it is in all cases, but it worked in my situation.
                    > private static AutoResetEvent AllThreadsDone = new
                    > AutoResetEvent( false);
                    >
                    > public static bool WaitForThreadPo olIdle(int timeout)[/color]

                    <cut>

                    Hi, thanks for the suggestion. I have gone the "counting semaphore" route. I
                    just thought the thread-pool might offer an in-built wait-method or
                    something for that functionality.

                    Peter


                    Comment

                    • Peter Kirk

                      #11
                      Re: ThreadPool wait for threads to finish


                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i en meddelelse
                      news:MPG.1d2b9e 4ddda0de8598c3c f@msnews.micros oft.com...[color=blue]
                      > <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                      > dot.state.fl.us >> wrote:[color=green]
                      >> If you want to execute them in sequence what you need to make a thread
                      >> for?[/color]
                      >
                      > He didn't say he wanted to execute them in sequence - he said he wanted
                      > to wait until they were all done. That's perfectly reasonable. For
                      > instance, you might be writing a web app which has to query several
                      > different data sources, and you want to do that concurrently, but you
                      > can't actually complete the request until all the separate queries are
                      > complete.[/color]

                      Yes, I am actually querying a search engine with a large number of search
                      requests while I process a large batch of documents. I'm hoping that by
                      splitting the batch of documents up into a number of threads each performing
                      searches on a smaller subset of documents then I can increase throughput
                      (actually I know I can), but I need to wait until all the searches/threads
                      are complete before I continue processing the documents batch.

                      Peter


                      Comment

                      • Peter Kirk

                        #12
                        Re: ThreadPool wait for threads to finish


                        "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i en meddelelse
                        news:MPG.1d2b8b 8a5773381898c3c d@msnews.micros oft.com...[color=blue]
                        > Shardool Karnik <TheOutlander@g mail.com> wrote:[color=green]
                        >> have each thread report to the main thread upon completion and then check
                        >> the thread count at that point to see if others are still
                        >> running....inst ead
                        >> of a while loop[/color]
                        >
                        > That would work - or make each thread "produce" on a semaphore, and
                        > wait (from the main thread) until the semaphore had been hit the
                        > appropriate number of times.[/color]

                        This is the route I have taken (a "counting semaphore", which I increment
                        each time I start a thread, and which each thread decrements when finished -
                        the main thread then waits until the count is 0).

                        Actually I didn't quite understand the first proposal. By "report to the
                        main thread", is that an event or some other sort of messaging mechanism
                        which the main thread listens for - obviously some sort of non-busy
                        listening?

                        Thanks,
                        Peter


                        Comment

                        Working...