Лабораторна робота
Синхронізація доступу потоків до загального ресурсу.
Результат:
Текст основної програми:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TForm1 *Form1;
Rus1 *P1=new Rus1 (true);
Rus2 *P2=new Rus2 (true);
Rus3 *P3=new Rus3 (true);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
if (Form1->CheckBox1->Checked==true)
{ Edit1->Text=" ";
Edit2->Text=" ";
Edit3->Text=" ";
Form1->Edit1->Enabled=true;
Form1->Edit2->Enabled=true;
Form1->Edit3->Enabled=true;
Form1->Repaint();
}
else
{Edit1->Text=300;
Edit2->Text=300;
Edit3->Text=300;
Form1->Edit1->Enabled=false;
Form1->Edit2->Enabled=false;
Form1->Edit3->Enabled=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Rus1 *P1=new Rus1 (true);
Rus2 *P2=new Rus2 (true);
Rus3 *P3=new Rus3 (true);
Form1->Button2->Enabled=true;
Form1->Button3->Enabled=true;
Form1->Button4->Enabled=true;
P1->Resume();
P2->Resume();
P3->Resume();
Form1->PaintBox1->Canvas->Pen->Color = clBtnFace;
Form1->PaintBox1->Canvas->Brush->Color = clBtnFace;
Form1->PaintBox1->Canvas->Rectangle(0,0,800,500);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
P1->Suspend();
P2->Suspend();
P3->Suspend();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
P1->Resume();
P2->Resume();
P3->Resume();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
P1->Terminate();
P2->Terminate();
P3->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
if (Form1->CheckBox1->Checked)
{ Edit1->Text=" ";
Edit2->Text=" ";
Edit3->Text=" ";
Form1->Edit1->Enabled=true;
Form1->Edit2->Enabled=true;
Form1->Edit3->Enabled=true;
Form1->Repaint();
}
else
{Edit1->Text=300;
Edit2->Text=300;
Edit3->Text=300;
Form1->Edit1->Enabled=false;
Form1->Edit2->Enabled=false;
Form1->Edit3->Enabled=false;
}
}
//---------------------------------------------------------------------------
Потік 1:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Unit2.h"
#include "Unit1.h"
#pragma package(smart_init)
//void Ghost(void);
//---------------------------------------------------------------------------
__fastcall Rus1::Rus1(bool CreateSuspended)
: TThread(CreateSuspended)
{
// FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
//void Ghost(void)
//--------
void __fastcall Rus1::Ghost1(void)
{
int dx1=1,prx1;
int dx2=1,prx2;
int X1=20,X2=60;
int Y1,Y2;
int f=25;int g=0;
for (int i=1;i<Form1->Edit3->Text;i++)
{
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->CGauge3->Progress=i;
Form1->PaintBox1->Canvas->Pen->Color = clGreen;
Form1->PaintBox1->Canvas->Brush->Color = clGreen;
Y1=(70*sin(i*0.07)+440);
Y2=(70*sin(i*0.07)+480);
Form1->PaintBox1->Canvas->Rectangle(X1,Y1,X2,Y2);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Unlock();
} Sleep (1);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->PaintBox1->Canvas->Pen->Color = clBtnFace;
Form1->PaintBox1->Canvas->Brush->Color = clBtnFace;
Form1->PaintBox1->Canvas->Rectangle(X1,Y1,X2,Y2);
//check for boundary
prx2=0;
if ((X2<5) || (X2>690)) prx2=1;
if (prx2==1) dx2=-dx2;
X2+=dx2;
//check for boundary
prx1=0;
if ((X1<5) || (X1>690)) prx1=1;
if (prx1==1)
dx1=-dx1;
X1+=dx1 ;
if (f==30)g=1;
if (g==1) f=f-1;
if (f==0) g=0;
if (g==0) f=f+1;
if (Form1->RadioButton2->Checked)
Form1->PaintBox1->Canvas->Unlock();
}
}
void __fastcall Rus1::Execute()
{ if (Form1->RadioButton1->Checked)
{ // while (!Terminated)
Synchronize(Ghost1);
}
if (Form1->RadioButton3->Checked)
Ghost1();
if (Form1->RadioButton2->Checked)
Ghost1();
}
//---------------------------------------------------------------------------
Потік 2:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall Rus2::Rus2(bool CreateSuspended)
: TThread(CreateSuspended)
{
//FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall Rus2::Ghost2(void)
{
Form1->CGauge1->MaxValue=Form1->Edit2->Text.ToInt()-1;
Form1->CGauge2->MaxValue=Form1->Edit2->Text.ToInt()-1;
Form1->PaintBox1->Canvas->Pen->Color = clFuchsia;
Form1->PaintBox1->Canvas->Brush->Color = clFuchsia;
Form1->PaintBox1->Canvas->MoveTo(0,0);
Form1->PaintBox1->Canvas->LineTo(690,0);
Form1->PaintBox1->Canvas->LineTo(690,550);
Form1->PaintBox1->Canvas->LineTo(0,550);
Form1->PaintBox1->Canvas->LineTo(0,0);
Form1->PaintBox1->Canvas->MoveTo(0,180);
Form1->PaintBox1->Canvas->LineTo(690,180);
Form1->PaintBox1->Canvas->MoveTo(0,360);
Form1->PaintBox1->Canvas->LineTo(690,360);
int dx1=1,dy1=1,prx1,pry1;
int dx2=1,dy2=1,prx2,pry2;
int dx3=1,dy3=1,prx3,pry3;
int dx4=1,dy4=1,prx4,pry4;
int n1,n2,n3,n4,n5,n6,n7,n8;
int v1=random(2),v2=random(2),v3=random(2);
int v4=random(2),v5=random(2),v6=random(2);
int v7=random(2),v8=random(2);
if (v1==1) n1=-1; else n1=1;
if (v2==1) n2=-1; else n2=1;
if (v3==1) n3=-1; else n3=1;
if (v4==1) n4=-1; else n4=1;
if (v5==1) n5=-1; else n5=1;
if (v6==1) n6=-1; else n6=1;
if (v7==1) n7=-1; else n7=1;
if (v8==1) n8=-1; else n8=1;
randomize();
int Y1,Y2,Y3,Y4;
int X1=random(690),X2=random(690),X3=random(690),X4=random(690);
int X5=180+random(100),X6=180+random(100);
int X7=180+random(100),X8=180+random(100);
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
for (int i=1;i<Form1->Edit2->Text;i++)
{
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->CGauge2->Progress=i;
Form1->PaintBox1->Canvas->Pen->Color = clRed;
Form1->PaintBox1->Canvas->Brush->Color = clRed;
Y1=(70*sin(i*0.07)+265);
Y2=(70*sin(i*0.05)+265);
Y3=(70*sin(i*0.06)+265);
Y4=(70*sin(i*0.07)+265);
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
Form1->PaintBox1->Canvas->LineTo(X2,Y2);
Form1->PaintBox1->Canvas->LineTo(X3,Y3);
Form1->PaintBox1->Canvas->LineTo(X4,Y4);
Form1->PaintBox1->Canvas->LineTo(X1,Y1);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Unlock();
} Sleep (10);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->PaintBox1->Canvas->Pen->Color = clBtnFace;
Form1->PaintBox1->Canvas->Brush->Color = clBtnFace;
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
Form1->PaintBox1->Canvas->LineTo(X2,Y2);
Form1->PaintBox1->Canvas->LineTo(X3,Y3);
Form1->PaintBox1->Canvas->LineTo(X4,Y4);
Form1->PaintBox1->Canvas->LineTo(X1,Y1);
X2+=dy2*n2;
//check for boundary
pry2=0;
if ((X2<5) || (X2>175)) pry2=1;
if (pry2==1) dy2=-dy2;
X1+=dy1*n4;
//check for boundary
pry1=0;
if ((X1<5) || (X1>690)) pry1=1;
if (pry1==1) dy1=-dy1;
X3+=dy3*n6;
//check for boundary
pry3=0;
if ((X3<5) || (X3>690)) pry3=1;
if (pry3==1) dy3=-dy3;
X4+=dy4*n8;
//check for boundary
pry4=0;
if ((X4<5) || (X4>690)) pry4=1;
if (pry4==1) dy4=-dy4;
if (Form1->RadioButton2->Checked)
Form1->PaintBox1->Canvas->Unlock();
}
}
void __fastcall Rus2::Execute()
{
if (Form1->RadioButton1->Checked)
{ // while (!Terminated)
Synchronize(Ghost2);
} if (Form1->RadioButton3->Checked)
Ghost2();
if (Form1->RadioButton2->Checked)
Ghost2();
}
//---------------------------------------------------------------------------
Потік 3:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include "Unit4.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall Rus3::Rus3(bool CreateSuspended)
: TThread(CreateSuspended)
{
//FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall Rus3::Ghost3(void)
{
Form1->CGauge3->MaxValue=Form1->Edit3->Text.ToInt()-1;
int dx1=1,dy1=1,prx1,pry1;
int dx2=1,dy2=1,prx2,pry2;
int dx3=1,dy3=1,prx3,pry3;
int dx4=1,dy4=1,prx4,pry4;
int n1,n2,n3,n4,n5,n6,n7,n8;
int v1=random(2),v2=random(2),v3=random(2);
int v4=random(2),v5=random(2),v6=random(2);
int v7=random(2),v8=random(2);
if (v1==1) n1=-1; else n1=1;
if (v2==1) n2=-1; else n2=1;
if (v3==1) n3=-1; else n3=1;
if (v4==1) n4=-1; else n4=1;
if (v5==1) n5=-1; else n5=1;
if (v6==1) n6=-1; else n6=1;
if (v7==1) n7=-1; else n7=1;
if (v8==1) n8=-1; else n8=1;
randomize();
int Y1,Y2,X3=25+random(100);
int X1=random(690),X2=random(690);
int X4=25+random(100);
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
for (int i=1;i<Form1->Edit2->Text;i++)
{
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->CGauge2->Progress=i;
Form1->PaintBox1->Canvas->Pen->Color = clBlue;
Form1->PaintBox1->Canvas->Brush->Color = clBlue;
Y1=(70*sin(i*0.07)+90);
Y2=(70*sin(i*0.05)+90);
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
Form1->PaintBox1->Canvas->LineTo(X2,Y2);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Unlock();
}
Sleep (10);
if (Form1->RadioButton2->Checked)
{Form1->PaintBox1->Canvas->Lock();
}
Form1->PaintBox1->Canvas->Pen->Color = clBtnFace;
Form1->PaintBox1->Canvas->Brush->Color = clBtnFace;
Form1->PaintBox1->Canvas->MoveTo(X1,Y1);
Form1->PaintBox1->Canvas->LineTo(X2,Y2);
X2+=dy2*n2;
//check for boundary
pry2=0;
if ((X2<5) || (X2>690)) pry2=1;
if (pry2==1) dy2=-dy2;
X1+=dy1*n4;
//check for boundary
pry1=0;
if ((X1<5) || (X1>690)) pry1=1;
if (pry1==1) dy1=-dy1;
if (Form1->RadioButton2->Checked)
Form1->PaintBox1->Canvas->Unlock();
}
}
void __fastcall Rus3::Execute()
{
if (Form1->RadioButton1->Checked)
{ // while (!Terminated)
Synchronize(Ghost3);
} if (Form1->RadioButton3->Checked)
Ghost3();
if (Form1->RadioButton2->Checked)
Ghost3();
}
//---------------------------------------------------------------------------