In this tutorial, multithreaded programming is explained using an example Counter Thread.
What is multithreading ?
A thread is basically a path of execution through a program. It is also the smallest unit of execution that Win32 schedules. A thread consists of a stack, the state of the CPU registers and an entry in the execution list of the system scheduler. Each thread shares all of the process's resources.
A "process" is an executing instance of an application. For example, when you double-click the Notepad icon, you start a process that runs Notepad. A "thread" is a path of execution within a process. When you start Notepad, the operating system creates a process and begins executing the primary thread of that process. When this thread terminates, so does the process. This primary thread is supplied to the operating system by the startup code in the form of a function address.
You can create additional threads in your application if you wish. You may want to do this to handle background or maintenance tasks when you don't want the user to wait for them to complete. All threads in . NET applications are represented by System.Threading namespace.
System.Threading - provides classes and interfaces that enable multithreaded programming. This namespace includes a ThreadPool class that manages groups of threads, a Timer class that enables a delegate to be called after a specified amount of time and a Mutex class for synchronizing mutually exclusive threads. System.Threading also provides classes for thread scheduling and wait notification.
A process consists of one or more threads and the code, data and other resources of a program in memory. Typical program resources are open files, semaphores and dynamically allocated memory. A program executes when the system scheduler gives one of its threads execution control. The scheduler determines which threads should run and when they should run. Threads of lower priority may have to wait while higher priority threads complete their tasks. On multiprocessor machines, the scheduler can move individual threads to different processors to "balance" the CPU load.
Each thread in a process operates independently. Unless you make them visible to each other, the threads execute individually and are unaware of the other threads in a process. Threads sharing common resources, however, must coordinate their work by using semaphores or another method of inter process communication.
Some of the methods you can use to control individual threads are given below.
||Causes a thread to start running.|
||Pauses a thread for a specified time.|
||Pauses a thread when it reaches a safe point.|
||Stops a thread when it reaches a safe point.|
||Restarts a suspended thread.|
||Causes the current thread to wait for another thread to finish. If used with a timeout value, this method returns True if the thread finishes in the allotted time.|
Please go through MSDN for more information about threads. Click on the appropriate link below to see the video and the source code of the Counter Thread tutorial.