Simple comparison of single threaded and multi threaded design in C program

  • 2021-09-24 23:23:24
  • OfStack

Multithreading concept

1.1 A running application is treated as a process in the operating system, and a process can include multiple threads. Threads are the basic unit of processor time allocated by the operating system
2. Application domains are error-isolated and security-isolated, run in CLR, where each program domain is started by a single thread, but code in that program domain can create additional application domains and additional threads
3. The advantage of multithreading is that when one thread is blocked, CUP can run other threads without waiting, which greatly improves the execution efficiency of the program. The disadvantage is that threads need to occupy memory, The more threads, the more memory they occupy. Multi-threads need coordination and management, so they need CPU time to track threads. The access to shared resources between threads will affect each other, so it is necessary to solve the problem of contending for shared resources. Too many threads will also lead to more complicated control, which will eventually lead to defects in many programs.
4.1 A process can create multiple threads to execute part of the program code associated with the process, and the threads use Tread processing

C # Single-threaded versus multithreaded:

Single thread:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
 
namespace Stockes 
{ 
  public partial class DeletgateThread : Form 
  { 
    public DeletgateThread() 
    { 
      InitializeComponent(); 
      CheckForIllegalCrossThreadCalls = false;// Allow cross-thread calls  
    } 
    public delegate void writeTxt(char chr);// Defining Delegates  
    public writeTxt writetxt;// Declare delegation  
    public void write(string str, writeTxt writes)// Using delegates  
    { 
      for (int i = 0; i < str.Length; i++) 
      { 
        writes(str[i]); 
        DateTime now = DateTime.Now; 
        while (now.AddSeconds(1) > DateTime.Now) { } 
      } 
    } 
    private void text1(char chr) 
    { 
      textBox1.AppendText(chr.ToString()); 
    } 
    public void text2(char chr) 
    { 
      textBox2.AppendText(chr.ToString()); 
    } 
    private void stratWrite() 
    { 
    
      if (checkBox1.Checked) 
      { 
        textBox1.Clear(); 
        groupBox4.Text = " Running. . "; 
        groupBox2.Refresh(); 
        writetxt = new writeTxt(text1); 
        write(textBox3.Text.Trim(),writetxt); 
      } 
      if(checkBox2.Checked) 
      { 
        textBox2.Clear(); 
        groupBox5.Text = " Running. . "; 
        groupBox3.Refresh(); 
        writetxt = new writeTxt(text2); 
        write(textBox3.Text.Trim(),writetxt); 
      } 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      Thread tr = new Thread(new ThreadStart(stratWrite));// Create Thread  
      tr.Start();// Startup thread  
    } 
     
  } 
} 
 


Multithreaded, concurrent tasks:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
 
namespace Stockes 
{ 
  public partial class DeletgateThread : Form 
  { 
    public DeletgateThread() 
    { 
      InitializeComponent(); 
      CheckForIllegalCrossThreadCalls = false;// Allow cross-thread calls  
    } 
    public delegate void writeTxt(char chr);// Defining Delegates  
    public writeTxt writetxt;// Declare delegation  
    public void write(string str, writeTxt writes)// Using delegates  
    { 
      for (int i = 0; i < str.Length; i++) 
      { 
        writes(str[i]); 
        DateTime now = DateTime.Now; 
        while (now.AddSeconds(1) > DateTime.Now) { } 
      } 
    } 
    private void text1(char chr) 
    { 
      textBox1.AppendText(chr.ToString()); 
    } 
    public void text2(char chr) 
    { 
      textBox2.AppendText(chr.ToString()); 
    } 
    private void stratWrite() 
    { 
      if (checkBox1.Checked) 
      { 
        textBox1.Clear(); 
        textBox1.Refresh(); 
        groupBox4.Text = " Running. . "; 
        groupBox2.Refresh(); 
        writetxt = new writeTxt(text1); 
        write(textBox3.Text.Trim(),writetxt); 
      } 
    } 
    private void stratwrite1() 
    { 
      if (checkBox2.Checked) 
      { 
        textBox2.Clear(); 
        textBox2.Refresh(); 
        groupBox5.Text = " Running. . "; 
        groupBox3.Refresh(); 
        writetxt = new writeTxt(text2); 
        write(textBox3.Text.Trim(), writetxt); 
      } 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      Thread tr = new Thread(new ThreadStart(stratWrite));// Create Thread  
      tr.Start();// Startup thread  
      Thread tr1 = new Thread(new ThreadStart(stratwrite1));// Create the 2 Threads  
      tr1.Start();// Startup thread  
    } 
     
  } 
} 


Related articles: