Why does my single-threaded program have multiple threads?

Raymond Chen

Raymond

You’ve written a simple single-threaded program, but when you look in Task Manager, it says that the program has two or even more threads. What’s going on?

Even though your program doesn’t create any threads, a library used by your program might create threads, and the system itself might create threads.

For example, if you call the SHFile­Operation function to copy some files, the shell may create additional threads to assist with the file copy operation. For example, the progress UI could be shown on the UI thread, with a separate thread used to perform the disk access.

Even after the multithreaded operation is complete, you may see threads lingering in the process because the multithreaded operation may have used the thread pool. Every process has a default thread pool which is created upon demand, and is destroyed at process termination.

If you are a console application, then the system creates an additional thread in your process in order to handle and deliver console control notifications.

In more recent versions of Windows 10 (I forget exactly when it started), the loader takes advantage of the thread pool to speed up loading DLLs into memory. This means that in practice, by the time the first line of code in your application starts to execute, the process default thread pool has already been created in order to load the DLLs your application uses.

 

2 comments

Comments are closed.

  • Avatar
    dcandy@gmail.com

    I wrote a VB6 program that was one thread as expected. The next version uses CreateWindowW on RICHEDIT50W to get a more modern version and Unicode version of VB6’s RichText box control. It is now a 5 thread program for the first minute then 4 threads.

    This prompted me to have a look at it’s replacement (VB.NET using RICHEDIT50W and TOM). It has 9. Although it has declined to 6 as I wrote this.

    One day I hope to actually see the IME windows that haunt my programs.

  • Avatar
    Линда Кайе

    Long time ago I wrote VB6 (yeah) app which shows all restore points in single list and create new restore point via WMI. Last operation often leads to new thread creation, and it doesn’t terminates on exit. So my app just hangs without UI =_=