1

I have a question of threads that have been bugging me recently. Take a look at this example C# code below.

 public void threading()
 {
     for(int count =0; count< 4; count++)
     {
        Thread childThread = new Thread(childThread);
        childThread.start()
     }

     return;
 }

public void childThread()
{
 // Do alot of work 
}

As there is a return statement right after the loop within the threading method, would threading execute the return statement before the all the child threads finish executing? I read somewhere that threads are different from forks as they do not create a separate memory region so where would a dead thread end up in?

Cœur
  • 37,241
  • 25
  • 195
  • 267
weejing
  • 173
  • 2
  • 12
  • 1
    Possible duplicate of [C# Thread object lifetime](http://stackoverflow.com/questions/3699147/c-sharp-thread-object-lifetime) – zerkms Sep 08 '16 at 04:59
  • "would `threading` execute the return statement before the all the child threads finish executing?" - Yes, as it is running in a separate thread. – Enigmativity Sep 08 '16 at 05:08
  • What do you mean by "dead thread"? – Enigmativity Sep 08 '16 at 05:08
  • It is fire-and-forget code. Very rarely correct, you have no idea when the threads are completed and very little hope to correctly deal with errors. And it made you ask this question. Do consider the Task class. – Hans Passant Sep 08 '16 at 05:42

2 Answers2

1

would threading execute the return statement before the all the child threads finish executing?

Maybe Yes. Maybe No. It all depends how long your childThread method takes to execute. In case your childThread method is really less time taking then it can happen that all four threads finish before return statement gets executed in threading method.

On the other side if it takes really long time then your threads can continue executing asynchronously even after threading method has finished executing or has returned to Main.

One additional thing you need to know:

By default all these threads they are creating are background threads. So they will exist as long as your process is alive. If your main GUI thread is going to end then these four threads will also go for a toss and will be aborted. So atleast one foreground thread must be alive for your four threads to continue executing childThread method to run to completion.

As far as memory is concerned - Each thread that gets created has its own stack memory area but they share common heap memory. Also, be it stack memory or heap memory of your thread it will certainly be under the periphery of your process's own address space.

RBT
  • 24,161
  • 21
  • 159
  • 240
1

If you want to enforce that all child threads terminate before your threading method returns, you can use the Join method on the threads, e.g.

public void Threading()
{
    List<Thread> threads = new List<Thread>();

    // start all threads
    for(int count =0; count< 4; count++)
    {
        Thread childThread = new Thread(ChildThread);
        childThread.start();
        threads.Add(thread);
    }

    // block until all threads have terminated
    for(int count =0; count< 4; count++)
    {
        threads[count].Join();
    }

    // won't return until all threads have terminated
    return;
}

public void ChildThread()
{
    // Do alot of work 
}
Curtis Lusmore
  • 1,822
  • 15
  • 16
  • Great. How does it address _"would threading execute the return statement before the all the child threads finish executing?"_ though? :) –  Sep 08 '16 at 05:28
  • I appreciate that this doesn't answer the question as asked literally. Another interpretation of a "will X do Y?" question is "how can I make X do Y?", which is what this answers ("how can I make it so that the method returns only after all the child threads finish executing?"). – Curtis Lusmore Sep 08 '16 at 05:37