A thread and a process are both independent sequences of execution, but they have some key differences. Here are some of them:
- A process has its own memory space, while a thread shares the memory space of its parent process.
- A process can create multiple threads, but a thread cannot create other threads or processes.
- A process can communicate with other processes using inter-process communication (IPC) mechanisms, such as pipes, sockets, or shared memory. A thread can communicate with other threads of the same process using shared variables or synchronization primitives, such as locks, semaphores, or condition variables.
- A process is more expensive to create and terminate than a thread, as it involves allocating and deallocating memory and resources. A thread is cheaper to create and terminate, as it only requires a stack and a thread control block (TCB).
- A process is more expensive to switch than a thread, as it involves saving and restoring the entire context of the process, including registers, program counter, stack pointer, etc. A thread is cheaper to switch, as it only requires saving and restoring a few registers and the stack pointer.
- 
- A process is isolated from other processes and protected by the operating system. A thread is not isolated from other threads and can be affected by their actions. For example, if one thread crashes or modifies a shared variable, it can cause problems for other threads.
These are some of the main differences between a thread and a process. Depending on the application and the operating system, one may be more suitable than the other. Threads are useful for parallelizing tasks within a single program, while processes are useful for running multiple programs concurrently.
Thread vs Process:
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Overview:
Today I'll show you how to working with Thread, Process and Thread Pool in C#.See.
- How to create Thread
- Property
- Common Method
- Foreground and Background Thread
- Thread Pool
Create Thread with source code CSharp (demo)
This demo description about two function with params- display function: no params
- display function: with 1 param (Objects)
- showMsg: with param (string)
Source Code:
/* *************************************************************
 * Author:          Zidane (huuvi168@gmail.com)
 * Last Modified:   20150630
 * *************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadDemo
{
    public class clsThread
    {
        public static void run()
        {
            // Thread with no params
            Thread thread1 = new Thread(() => display());
            thread1.Start();
            
            // Thread with param: string
            Thread thread2 = new Thread(() => showMsg("[Learn-tech-tips] 
                                 Thread with one param(string)"));
            thread2.Start();            
            // Thread with param: Object
            clsStudent student = new clsStudent();
            student.Id = 168;
            student.Name = "[Learn-tech-tips] Thread with param(Objects)";            
            Thread thread3 = new Thread(() => display(student));
            thread3.Start();
            
        }     
        public static void display()
        {
            Console.WriteLine("[Learn-tech-tips] Thread with no params");
            Thread.Sleep(90000);
        }
        public static void showMsg(string message)
        {
            Console.WriteLine(message);
        }
        public static void display(clsStudent student)
        {
            Console.WriteLine(student.Id.ToString());
            Console.WriteLine(student.Name.ToString());
        }
    }
    public class clsStudent
    {
        private int id;
        private string name;
      
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
    }    
}
Result:
Using Common Method on Thread (demo)
We have 3 function: After thread1, thread2 start finished, thread3 will be startSource code:
/* *************************************************************
 * Author:          Zidane (huuvi168@gmail.com)
 * Last Modified:   20150630
 * *************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadDemo
{
    public class clsCommonMethod
    {
        public static void run()
        {
            Thread thread1 = new Thread(MethodA);
            Thread thread2 = new Thread(MethodB);
            Thread thread3 = new Thread(MethodC);
            thread1.Start();
            thread2.Start();
                        
            thread2.Join();
            // After thread2 done, thread3 can start
            thread3.Start();
        }
        public static void MethodA()
        {
            for (int i = 0; i < 100; i++) Console.Write("0");
        }
        public static void MethodB()
        {
            for (int i = 0; i < 100; i++) Console.Write("1");
        }
        public static void MethodC()
        {
            for (int i = 0; i < 100; i++) Console.Write("2");
        }
    }
}
Result:
Thread Locking
If we don't use lock API, the index will be calculator don't correct.So we need using SynObj Object for locking calculator index.
Source code:
/* *************************************************************
 * Author:          Zidane (huuvi168@gmail.com)
 * Last Modified:   20150630
 * *************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadPoolDemo
{
    public class clsThreadLocking
    {
        static int index = 0;
        static object syncObj = new object();
        public static void run()
        {
            Thread thread1 = new Thread(inputIncrease);
            Thread thread2 = new Thread(inputDecrease);
            thread1.Start();
            thread2.Start();            
        }
        static void inputIncrease()
        {
            for (int i = 0; i < 80; i++)
            {
                lock (syncObj)  // local obj
                {
                    index++;
                    Consdeole.Write(index + "\t");                    
                }
            }
        }
        static void inputDecrease()
        {
            for (int i = 0; i < 80; i++)
            {
                lock (syncObj)
                {                    
                    index--;
                    Console.Write(index + "\t");    
                }
            }
        }
    }
}
View more Thread pool from this link :)
Are you interested in topic Understanding Thread and Process and Thread Pool in C# from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it
Webzone Tech Tips Zidane
 
 
 
 
 
 
 
 




 
 
 
 
 
 
 
 
 
 
