Logo GenDocs.ru

Поиск по сайту:  

Загрузка...

Синхронизация доступа потоков к общему ресурсу - файл 1.doc


Синхронизация доступа потоков к общему ресурсу
скачать (145 kb.)

Доступные файлы (1):

1.doc145kb.08.12.2011 21:54скачать

содержание
Загрузка...

1.doc

Реклама MarketGid:
Загрузка...
Лабораторная работа № 2

Синхронизация доступа потоков к общему ресурсу
Курск 2008

Задание. Разработать программу моделирования поведения трех типов объектов, организовать отображение поведения каждого объекта с помощью отдельного потока на общей канве. Функционирование потоков реализовать с помощью функций Win32 API.


Вариант

A

c1

e1

f1

B

c2

e2

f2

C

c3

e3

f3

D

15

12

1

7

4

13

4

3

6

5

3

4

1

2





^ Текст программы:

TForm1 *Form1;

TForm2 *Form2;

TForm3 *Form3;

const unsigned short MAX_THREADS_COUNT = 15;

const unsigned short USE_SEMAPHORE = 0;

const unsigned short USE_MUTEX = 1;

const unsigned short USE_EVENT = 2;

unsigned short current_threads_count(0);

HANDLE MainBlock[MAX_THREADS_COUNT];

int flags[MAX_THREADS_COUNT];
//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)
{

Form3 = new TForm3(this);

Form3->Tag = 0;

Form3->ShowModal();

Form2 = new TForm2(this);

switch (Form3->Tag)

{

case 0: {

InitializeMySemaphore();

setTypeSynchronize(USE_SEMAPHORE);

break;

}
case 1: {

InitializeMyMutex();

setTypeSynchronize(USE_MUTEX);

break; }

case 2: {

InitializeMyEvent();

setTypeSynchronize(USE_EVENT);
break; }


}

delete Form3;

Form2->Show();

}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)

{
if (current_threads_count > MAX_THREADS_COUNT)

{

ShowMessage("Больше потоков создавать нельзя!!!");

return;

}

switch (RGChooseType->ItemIndex)

{

case 0:{

MainBlock[current_threads_count]= CreateThread(0,0,ObjectA,Form2->Handle,CREATE_SUSPENDED,NULL);

ListBox1->AddItem("ПотокА"+IntToStr(current_threads_count),NULL);

break;}

case 1:{

MainBlock[current_threads_count]= CreateThread(0,0,ObjectB,Form2->Handle,CREATE_SUSPENDED,NULL);

ListBox1->AddItem("ПотокB"+IntToStr(current_threads_count),NULL);

break;}

case 2:{

MainBlock[current_threads_count]= CreateThread(0,0,ObjectC,Form2->Handle,CREATE_SUSPENDED,NULL);

ListBox1->AddItem("ПотокC"+IntToStr(current_threads_count),NULL);

break;}
}
ListBox1->ItemIndex=current_threads_count;
switch (RGChoosePriority->ItemIndex)

{

case 0:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_TIME_CRITICAL);

break;}

case 1:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_HIGHEST);

break;}

case 2:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_ABOVE_NORMAL);

break;}

case 3:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_NORMAL);

break;}

case 4:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_BELOW_NORMAL);

break;}

case 5:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_LOWEST);

break;}

case 6:{

SetThreadPriority(MainBlock[current_threads_count],THREAD_PRIORITY_IDLE);

break;}

} ;
flags[current_threads_count]=0;

ResumeThread(MainBlock[current_threads_count]);

current_threads_count++;
}

//---------------------------------------------------------------------------


void __fastcall TForm1::RGChoosePriorityClick(TObject *Sender)

{ switch (RGChoosePriority->ItemIndex)

{

case 0:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_TIME_CRITICAL);
break;}

case 1:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_HIGHEST);

break;}

case 2:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_ABOVE_NORMAL);

break;}

case 3:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_NORMAL);

break;}

case 4:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_BELOW_NORMAL);

break;}

case 5:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_LOWEST);

break;}

case 6:{

SetThreadPriority(MainBlock[ListBox1->ItemIndex],THREAD_PRIORITY_IDLE);

break;}

}


}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)

{

if (flags[ListBox1->ItemIndex]!=2 )

if (flags[ListBox1->ItemIndex ]== 0)

{
SuspendThread(MainBlock[ListBox1->ItemIndex]);

flags[ListBox1->ItemIndex]=1;

Button2->Caption="Продолжить";

}

else if (flags[ListBox1->ItemIndex] == 1)

{
ResumeThread(MainBlock[ListBox1->ItemIndex]);

flags[ListBox1->ItemIndex]=0;

Button2->Caption="Пауза";
}

}

//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)

{

if (flags[ListBox1->ItemIndex ]!= 2){

if (flags[ListBox1->ItemIndex ]== 1)

{
Button2->Caption="Продолжить";

}

else if (flags[ListBox1->ItemIndex] == 0)

{
Button2->Caption="Пауза";
}
int pri;

pri = GetThreadPriority(MainBlock[ListBox1->ItemIndex]);

switch (pri)

{

case THREAD_PRIORITY_TIME_CRITICAL:{

RGChoosePriority->ItemIndex=0;

break;}

case THREAD_PRIORITY_HIGHEST:{

RGChoosePriority->ItemIndex=1;

break;}
case THREAD_PRIORITY_ABOVE_NORMAL:{

RGChoosePriority->ItemIndex=2;

break;}
case THREAD_PRIORITY_NORMAL:{

RGChoosePriority->ItemIndex=3;

break;}
case THREAD_PRIORITY_BELOW_NORMAL:{

RGChoosePriority->ItemIndex=4;

break;}
case THREAD_PRIORITY_LOWEST:{

RGChoosePriority->ItemIndex=5;

break;}
case THREAD_PRIORITY_IDLE:{

RGChoosePriority->ItemIndex=6;

break;}
}}

}

//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)

{

if (flags[ListBox1->ItemIndex] != 2)

{

TerminateThread(MainBlock[ListBox1->ItemIndex],NULL);

flags[ListBox1->ItemIndex]=2;

Signal_after_destroying();

FillByWhite(Form2->Handle);

}

}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)

{

for (int i = 0;i<MAX_THREADS_COUNT;i++)

{

if (MainBlock[i]) CloseHandle(MainBlock[i]);
}

CloseOpenedHandlers();

}

//---------------------------------------------------------------------------

#ifndef DrawFunctionsH

#define DrawFunctionsH

//---------------------------------------------------------------------------

#endif

#include <Controls.hpp>

#include <Math.h>

//---------------------------------------------------------------------------

DWORD WINAPI ObjectA(LPVOID Param);

DWORD WINAPI ObjectB(LPVOID Param);

DWORD WINAPI ObjectC(LPVOID Param);

void FillByWhite(void* Param);

void CloseOpenedHandlers();

void Signal_after_destroying();

void InitializeMySemaphore();

void InitializeMyMutex();

void InitializeMyEvent();

void setTypeSynchronize(int x);

//---------------------------------------------------------------------------


#pragma hdrstop
#include "DrawFunctions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

int speed = 2000;

int how_Synchron;

HANDLE MySemaphore, MyMutex, MyEvent;
void FillByWhite(void* Param)

{
HANDLE OBL(Param);

RECT R;

GetClientRect(OBL,&R);

int width;

int height;

width = R.right-R.left;

height = R.bottom-R.top;

HDC dc = GetDC(OBL);

HPEN BlackPen = CreatePen(psSolid,1,clWhite);

SelectObject(dc,BlackPen);

Rectangle(dc,0,0,width,height);

ReleaseDC(OBL,dc);

}


void CloseOpenedHandlers()

{

if (MySemaphore != NULL) CloseHandle(MySemaphore);

if (MyMutex != NULL) CloseHandle(MyMutex);

if (MyEvent != NULL) CloseHandle(MyEvent);

}
void Signal_after_destroying()

{

switch (how_Synchron)

{

case 0:{

ReleaseSemaphore(MySemaphore,1,NULL);

break;}

case 1:{

ReleaseMutex(MyMutex);

break;}

case 2:{

SetEvent(MyEvent);

break;}

}

}
void InitializeMySemaphore()

{

MySemaphore = CreateSemaphore(NULL,1,1,NULL);

}
void InitializeMyMutex()

{

MyMutex = CreateMutex(NULL,FALSE,NULL);

}
void InitializeMyEvent()

{

MyEvent = CreateEvent(NULL,false,true,NULL);

}
void setTypeSynchronize(int x)

{

how_Synchron = x;

}

;


DWORD WINAPI ObjectA(LPVOID Param)

{

int x0,xp,y0,d,yp;
HANDLE OBL(Param);

RECT R;

GetClientRect(OBL,&R);

int width;

int height;

int r;
width = R.right-R.left;

height = R.bottom-R.top;
d = 50;

x0 = random(width / 3 - d);

y0 = 5;

yp = y0;

xp = x0;
HDC dc = GetDC(OBL);


HPEN BlackPen = CreatePen(psSolid,1,clRed);

HPEN WhitePen = CreatePen(psSolid,1,clWhite);
float modPi;

modPi = 0;

int delay;

delay = 0;

int t,kr,kr2,V;

kr=2;

kr2 = 1;

V = 1;
t = 0;
while (true)

{

switch (how_Synchron)

{

case 0:{

WaitForSingleObject(MySemaphore,INFINITE);
break;}

case 1:{

WaitForSingleObject(MyMutex,INFINITE);

break;}

case 2:{

WaitForSingleObject(MyEvent,INFINITE);

break;}
}

if (delay == speed) {


delay = 0;

HDC dc = GetDC(OBL);

//-----------------------------------------

SelectObject(dc,WhitePen);
Ellipse(dc,xp,yp,xp+d,yp+d);
for (int i = 1;i<=8;i++)

{

MoveToEx(dc,xp+d/2 ,yp+d/2,NULL);

LineTo(dc,1.5*d*cos(M_PI/4*i+modPi)+(xp+d/2),1.5*d*sin(M_PI/4*i+modPi)+(yp+d/2));

}

V+=kr2;

if (V >= 3) kr2=-1;

else if (V <= 1) kr2=+1;

yp = yp+V;

xp = xp + kr;


if (xp>=width/3-d*2) kr=-2;

else if (xp<d) kr = 2;

modPi+=M_PI/64;

if (modPi == M_PI )modPi = 0;
t++;
if (yp > height)

{

t = 0;

xp = random(width / 3 - d);

yp = 5;

}
//-------------------------------------------

SelectObject(dc,BlackPen);

r = 25;

for (int i = 1;i<=8;i++)

{

MoveToEx(dc,xp+d/2 ,yp+d/2,NULL);

LineTo(dc,1.5*d*cos(M_PI/4*i+modPi)+(xp+d/2),1.5*d*sin(M_PI/4*i+modPi)+(yp+d/2));

}

Ellipse(dc,xp,yp,xp+d,yp+d);

ReleaseDC(OBL,dc);

}

else delay++;

switch (how_Synchron)

{

case 0:{

ReleaseSemaphore(MySemaphore,1,NULL);

break;}

case 1:{

ReleaseMutex(MyMutex);

break;}

case 2:{

SetEvent(MyEvent);

break;}
}
}

}
DWORD WINAPI ObjectB(LPVOID Param)

{

int x0,y0,d,yp;

d = 50;

HANDLE OBL(Param);

RECT R;

GetClientRect(OBL,&R);

int width;

int height;

width = R.right-R.left;

height = R.bottom-R.top;

x0 = random(width / 3)+width/3+d;

y0 = d;

yp = y0;

HDC dc = GetDC(OBL);

HPEN BlackPen = CreatePen(psSolid,1,clRed);

HPEN WhitePen = CreatePen(psSolid,1,clWhite);
int delay,mod;

delay = 0;

mod = d/2;

int t,kr;

t = 0;

kr = 1;
while (true)

{

switch (how_Synchron)

{

case 0:{

WaitForSingleObject(MySemaphore,INFINITE);

break;}

case 1:{

WaitForSingleObject(MyMutex,INFINITE);

break;}

case 2:{

WaitForSingleObject(MyEvent,INFINITE);

break;}
}
if (delay == speed) {
delay = 0;

HDC dc = GetDC(OBL);

//-----------------------------------------

SelectObject(dc,WhitePen);

MoveToEx(dc,x0,yp,NULL);

LineTo(dc,x0,yp+d);

LineTo(dc,x0+d,yp+d);

LineTo(dc,x0+d,yp);

LineTo(dc,x0,yp);


x0 += mod ;

yp -= mod ;
MoveToEx(dc,x0,yp,NULL);

LineTo(dc,x0,yp+d);

LineTo(dc,x0+d,yp+d);

LineTo(dc,x0+d,yp);

LineTo(dc,x0,yp);

LineTo(dc,x0-mod,yp+mod);

MoveToEx(dc,x0,yp+d,NULL);

LineTo(dc,x0-mod,yp+d+mod);

MoveToEx(dc,x0+d,yp+d,NULL);

LineTo(dc,x0+d-mod,yp+d+mod);

MoveToEx(dc,x0+d,yp,NULL);

LineTo(dc,x0+d-mod,yp+mod);
x0 -=mod;

yp +=mod;

yp = (y0+t+9.8*t*t/2)/1e2;

d =d+kr;

if (d > 75) kr=-1;

else if (d < 51) kr = 1;
t++;

if (yp > height)

{

t = 0;

x0 = random(width / 3)+width/3 - d;

yp = 5;

}
//-------------------------------------------

SelectObject(dc,BlackPen);

MoveToEx(dc,x0,yp,NULL);

LineTo(dc,x0,yp+d);

LineTo(dc,x0+d,yp+d);

LineTo(dc,x0+d,yp);

LineTo(dc,x0,yp);


x0 += mod ;

yp -= mod ;
MoveToEx(dc,x0,yp,NULL);

LineTo(dc,x0,yp+d);

LineTo(dc,x0+d,yp+d);

LineTo(dc,x0+d,yp);

LineTo(dc,x0,yp);
LineTo(dc,x0-mod,yp+mod);

MoveToEx(dc,x0,yp+d,NULL);

LineTo(dc,x0-mod,yp+d+mod);

MoveToEx(dc,x0+d,yp+d,NULL);

LineTo(dc,x0+d-mod,yp+d+mod);

MoveToEx(dc,x0+d,yp,NULL);

LineTo(dc,x0+d-mod,yp+mod);
x0 -=mod;

yp +=mod;

ReleaseDC(OBL,dc);

}

else delay++;

switch (how_Synchron)

{

case 0:{

ReleaseSemaphore(MySemaphore,1,NULL);

break;}

case 1:{

ReleaseMutex(MyMutex);

break;}

case 2:{

SetEvent(MyEvent);

break;}
}

}

}
DWORD WINAPI ObjectC(LPVOID Param)

{

int x0,y0,xp,r,yp;

r = 30;

float angle,trespin;

angle = 0;

trespin = 0;
HANDLE OBL(Param);

RECT R;

GetClientRect(OBL,&R);

int width;

int height;

width = R.right-R.left;

height = R.bottom-R.top;

x0 = 2*width/3 + width/6;

y0 = r+2;

xp = x0;

yp = y0;

HDC dc = GetDC(OBL);

HPEN BlackPen = CreatePen(psSolid,1,clBlue);

HPEN WhitePen = CreatePen(psSolid,1,clWhite);

int delay;

delay = 0;
int t;

t = 0;


while (true)

{

switch (how_Synchron)

{

case 0:{

WaitForSingleObject(MySemaphore,INFINITE);

break;}

case 1:{

WaitForSingleObject(MyMutex,INFINITE);

break;}

case 2:{

WaitForSingleObject(MyEvent,INFINITE);

break;}
}

if (delay == speed) {
delay = 0;

HDC dc = GetDC(OBL);

//-----------------------------------------

SelectObject(dc,WhitePen);


Ellipse(dc,xp-r,yp-r,xp+r,yp+r);

MoveToEx(dc,r*sin(trespin)+xp,r*cos(trespin)+yp,NULL);

LineTo(dc,r*sin(trespin+2.0/3*M_PI)+xp,r*cos(trespin+2.0/3*M_PI)+yp);

LineTo(dc,r*sin(trespin+4.0/3*M_PI)+xp,r*cos(trespin+4.0 /3*M_PI)+yp);

LineTo(dc,r*sin(trespin)+xp,r*cos(trespin)+yp);
angle+=M_PI / 64;

yp++;

xp = x0+2*r*sin(angle);

trespin+=M_PI/64;

if (angle >= 2*M_PI ) angle = 0;

if (trespin >= 2*M_PI ) trespin = 0;
if (yp > height)

{

x0 = 2*width/3 + width/6;

y0 = r+2;

xp = x0;

yp = y0;

}
//-------------------------------------------


SelectObject(dc,BlackPen);

Ellipse(dc,xp-r,yp-r,xp+r,yp+r);

MoveToEx(dc,r*sin(trespin)+xp,r*cos(trespin)+yp,NULL);

LineTo(dc,r*sin(trespin+2.0/3*M_PI)+xp,r*cos(trespin+2.0/3*M_PI)+yp);

LineTo(dc,r*sin(trespin+4.0/3*M_PI)+xp,r*cos(trespin+4.0 /3*M_PI)+yp);

LineTo(dc,r*sin(trespin)+xp,r*cos(trespin)+yp);
ReleaseDC(OBL,dc);


}

else delay++;

switch (how_Synchron)

{

case 0:{

ReleaseSemaphore(MySemaphore,1,NULL);

break;}

case 1:{

ReleaseMutex(MyMutex);

break;}

case 2:{

SetEvent(MyEvent);

break;}
}

}

}
^ Тест программы




Скачать файл (145 kb.)

Поиск по сайту:  

© gendocs.ru
При копировании укажите ссылку.
обратиться к администрации