Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2004

Tip: Looking for answers? Try searching our database.

Nested Threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Juan Carlos - 11 May 2004 23:26 GMT
Hi

I need to use Nested Threads, I can throw a parent thread and then throw his son threads, but my problem is to make a pause, because sometimes I can't throw all son threads, I have to throw for example three, then I wait to finish to execute them and then throw the last two threads. And the parent thread has to finish until all his son threads have been executed.

Is it possible to do it?

Regards
J.C.
David Browne - 11 May 2004 23:50 GMT
> Hi
>
> I need to use Nested Threads, I can throw a parent thread and then throw his son threads, but my problem is to make a pause, because sometimes I
can't throw all son threads, I have to throw for example three, then I wait
to finish to execute them and then throw the last two threads. And the
parent thread has to finish until all his son threads have been executed.

> Is it possible to do it?

Use Thread.Join. Like this:

using System;
using System.Threading;
public class Test
{
   [STAThread]
   static void Main(string[] args)
   {
     Thread[] childThreads = new Thread[3];
     childThreads[0] = new Thread( new ThreadStart(ChildThreadProc));
     childThreads[1] = new Thread( new ThreadStart(ChildThreadProc));
     childThreads[2] = new Thread( new ThreadStart(ChildThreadProc));

     Console.WriteLine("Starting threads.");
     foreach (Thread t in childThreads)
     {
       t.Start();
     }

     Console.WriteLine("Waiting for threads to finish.");
     foreach (Thread t in childThreads)
     {
       t.Join();
     }
     Console.WriteLine("All threads finished.");
   }

   static void ChildThreadProc()
   {
     Thread.Sleep(1000);
     Console.WriteLine("Thread Finished.");
   }
}
Juan Carlos - 12 May 2004 00:41 GMT
Thanks a lot David, I apreciate your help.

But now, my problem is when a parent Thread has a limit to throw its son threads. Let me explain:

The parent thread has a queue, where all its sons wait to be executed, but there is a limit, only three threads can be executed at the same time and the other two have to wait to one of them finish its execution to be executed. When it happens, I have to throw the next son in the queue.

Thread1 (parent)
   threadA (son)                             I can only execute the threads A, B and C
   threadB (son)                             D and E have to wait
   threadC (son)
   threadD (son)
   threadE (son)

For example If B have finished, I check my Limit and I see, there is a place and I can throw another thread, in this case will be D and E has to wait. I have A, C and D running. Now, If A have finished, I do the same and throw the last one, in this case E.

If all son threads have finished, the parent thread has to finish too.

Is it posible to do it?

Thanks again
J.C.

P.D. I'm sorry for my english, I try to explain me the best way.
Alvin Bruney [MVP] - 12 May 2004 04:45 GMT
This algorithm is hopeless complicated. Either use the thread pool or
explain why you need all this complicated stuff

Signature

Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok

> Thanks a lot David, I apreciate your help.
>
[quoted text clipped - 27 lines]
>
> P.D. I'm sorry for my english, I try to explain me the best way.
Jakob Christensen - 12 May 2004 07:56 GMT
Hey Juan,

One way is to use a thread pool as Alvin Bruney states.  Another way is to a synchronized queue containing some kind of objects containing information about the work each thread has to do.  Then you start three worker threads that pull items out of this queue and do the work they are supposed to do.  Meanwhile the main thread (your parent thread) can add new items to the queue.

But as Alvin says, this is a complicated scenario.  Do you really need to do this?

Regards, Jakob.
Andranik Khachatryan - 12 May 2004 17:51 GMT
  Hello,
 I am not sure that I fully understand the problem, but You could try something like this
Have a "synchronization event". Each thread that finishes it's work raises this event. The event handler determines that the number of currently working threads is less than maximum allowed, and starts one more of the available threads
The problem is that 2 threads might finish at same time, so they both will raise the event. So, You should use synchLocks when entering the event handler
A pseudocode would be something like thi
   void ThreadFinishedHandler(); //Raised every time a thread is finishe
   Queue threadQueue; // The queue of threads available for executio
   Object syncObj; // the object used for syncronizatio

So,
     1. Start the threads(suppose they are A, B, C
     2. Init the threads E, F, G as "waiting", put them into threadQueu
     3. Suppose thread B has finished executin
     3.1 Lock syncObjec
     3.2 Raise event ThreadFinished, which actually means executing ThreadFinishedHandler()
         ThreadFinishedHandler now examines threadQueue, sees that there are available threads there, and chooses
one of them by a special logic (say, it is G
     4. Start G, remove G from threadQueue.
Repea

Andranik Khachatrya
Jon Skeet [C# MVP] - 12 May 2004 08:12 GMT
> I need to use Nested Threads

Threads aren't nested - there's no hierarchy of threads.

> I can throw a parent thread and then throw his son threads

What do you mean by "throw" here? Do you mean "start"?

> but my problem is to make a pause, because sometimes I can't throw
> all son threads, I have to throw for example three, then I wait to
[quoted text clipped - 3 lines]
>
> Is it possible to do it?

Use Thread.Join to make one thread wait for another one to finish.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

andrea catto' - 12 May 2004 18:06 GMT
if in the framework there's still mutex and semaphores, that's the way to
go.

look for Semaphores.
they allow setting a count down, and whey will wait when they get back to
zero.
for example, you can create a semaphore with count 3...

> Hi
>
> I need to use Nested Threads, I can throw a parent thread and then throw his son threads, but my problem is to make a pause, because sometimes I
can't throw all son threads, I have to throw for example three, then I wait
to finish to execute them and then throw the last two threads. And the
parent thread has to finish until all his son threads have been executed.

> Is it possible to do it?
>
> Regards
> J.C.
Jon Skeet [C# MVP] - 13 May 2004 07:46 GMT
> if in the framework there's still mutex and semaphores, that's the way to
> go.
[quoted text clipped - 3 lines]
> zero.
> for example, you can create a semaphore with count 3...

The framework doesn't contain semaphores, but they're easy to write
from monitors.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.