Nested Threads

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Juan Carlos said:
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.");
}
}
 
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.
 
This algorithm is hopeless complicated. Either use the thread pool or
explain why you need all this complicated stuff
 
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.
 
Juan Carlos said:
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
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 to make one thread wait for another one to finish.
 
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
 
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...

Juan Carlos said:
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.
 
andrea catto' said:
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...

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