Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutting down a multithreaded application

I'm trying to write a ThreadManager for my C# application. I create several threads:
One thread for my text writer.
One thread that monitors some statistics.
Multiple threads to perform a large sequence of calculations (up to 4 threads per core and I run my app on a 2x quad core server).

My application normally runs for up to 24 hours at a time, so all the threads get created in the beginning and they persist through the entire time the app runs.

I want to have a single place where I "register" all of my treads and when the application is shutting down I simply invoke a method and it goes through all of the registered threads and shuts them down.

For that purpose I have devised the following class:

public class ThreadManager
{
    private static Object _sync = new Object();
    private static ThreadManager _instance = null;
    private static List<Thread> _threads;
    private ThreadManager()
    {
        _threads = new List<Thread>();
    }

    public static ThreadManager Instance
    {
        get
        {
            lock (_sync)
            {
                if (_instance == null)
                {
                    _instance = new ThreadManager();
                }
            }
            return _instance;
        }
    }

    public void AddThread(Thread t)
    {
        lock (_sync)
        {
            _threads.Add(t);
        }
    }

    public void Shutdown()
    {
        lock (_sync)
        {
            foreach (Thread t in _threads)
            {
                t.Abort(); // does this also abort threads that are currently blocking?
            }
        }
    }
}

I want to ensure that all of my threads are killed so the application can close properly and shutting down in the middle of some computation is just fine too. Should I be aware of anything here? Is this approach good given my situation?

like image 459
Kiril Avatar asked Feb 22 '10 22:02

Kiril


People also ask

How do I stop multithreading?

You can implement concurrent tasks by using coroutines. You then have to explicitly pass control (the cpu) to another coroutine. It won't be done automatically by an interrupt after a small delay.

What is an example of a multithreaded application?

Another example of a multithreaded program that we are all familiar with is a word processor. While you are typing, multiple threads are used to display your document, asynchronously check the spelling and grammar of your document, generate a PDF version of the document.

What is a multithreaded app?

A multi-threaded application is an application whose architecture takes advantage of the multi-threading provided by the operating system. Usually, these applications assign specific jobs to individual threads within the process and the threads communicate, through various means, to synchronize their actions.

How do you know if an application is multithreaded?

For Windows you can run the program and then open task manager by pressing ctrl+shift+esc keys. Look for processes with a number in parentheses after the name: That is the number of tasks the program currently has active. Single-threaded programs won't have multiple tasks.


2 Answers

If you set the threads to background threads, they will be killed when the application is shut down.

myThread.IsBackground = true;

obviously if you need the threads to finish before shutdown, this is not the solution you want.

like image 86
Nate Heinrich Avatar answered Oct 06 '22 03:10

Nate Heinrich


Aborting threads is what you do when all else fails. It is a dangerous thing to do which you should only do as a last resort. The correct way to do this is to make your threading logic so that every worker thread responds quickly and correctly when the main thread gives it the command to shut itself down.

Coincidentally, this is the subject of my blog this week.

http://blogs.msdn.com/ericlippert/archive/2010/02/22/should-i-specify-a-timeout.aspx

like image 45
Eric Lippert Avatar answered Oct 06 '22 03:10

Eric Lippert