How to resolve the algorithm Mutex step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mutex step by step in the Delphi programming language

Table of Contents

Problem Statement

A mutex (abbreviated Mutually Exclusive access) is a synchronization object, a variant of semaphore with k=1. A mutex is said to be seized by a task decreasing k. It is released when the task restores k. Mutexes are typically used to protect a shared resource from concurrent access. A task seizes (or acquires) the mutex, then accesses the resource, and after that releases the mutex. A mutex is a low-level synchronization primitive exposed to deadlocking. A deadlock can occur with just two tasks and two mutexes (if each task attempts to acquire both mutexes, but in the opposite order). Entering the deadlock is usually aggravated by a race condition state, which leads to sporadic hangups, which are very difficult to track down.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mutex step by step in the Delphi programming language

Source code in the delphi programming language

unit main;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms,
  System.SyncObjs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    mmo1: TMemo;
    btn1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FMutex: TMutex;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FMutex := TMutex.Create();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FMutex.Free;
end;

// http://edgarpavao.com/2017/08/07/multithreading-e-processamento-paralelo-no-delphi-ppl/
procedure TForm1.btn1Click(Sender: TObject);
begin
//Thread 1
  TThread.CreateAnonymousThread(
    procedure
    begin
      FMutex.Acquire;
      try
        TThread.Sleep(5000);
        TThread.Synchronize(TThread.CurrentThread,
          procedure
          begin
            mmo1.Lines.Add('Thread 1');
          end);
      finally
        FMutex.Release;
      end;
    end).Start;

  //Thread 2
  TThread.CreateAnonymousThread(
    procedure
    begin
      FMutex.Acquire;
      try
        TThread.Sleep(1000);
        TThread.Synchronize(TThread.CurrentThread,
          procedure
          begin
            mmo1.Lines.Add('Thread 2');
          end);
      finally
        FMutex.Release;
      end;
    end).Start;

  //Thread 3
  TThread.CreateAnonymousThread(
    procedure
    begin
      FMutex.Acquire;
      try
        TThread.Sleep(3000);
        TThread.Synchronize(TThread.CurrentThread,
          procedure
          begin
            mmo1.Lines.Add('Thread 3');
          end);
      finally
        FMutex.Release;
      end;
    end).Start;
end;

end.


  

You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the Julia programming language
You may also check:How to resolve the algorithm Animation step by step in the Racket programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Visual FoxPro programming language
You may also check:How to resolve the algorithm Bitmap step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the jq programming language