Поможем написать учебную работу
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.

Предоплата всего

Подписываем
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.
Предоплата всего
Подписываем
// Лабораторная работа № 2.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
//using namespace std;
HANDLE hthread;
int sub_res;
HANDLE hThread;
int sum_res;
DWORD WINAPI thr_procsub(LPVOID lpParameter)
{
sub_res=((int*)lpParameter)[0] - ((int*)lpParameter)[1];
return 1;
}
DWORD WINAPI thr_procsum( LPVOID lpParameter )
{
sum_res= ((int*)lpParameter)[0] + ((int*)lpParameter)[1];
return 2;
}
int main( int argc, char** argv )
{
DWORD thrsubId;
int vals[2];
vals[0] = 10;
vals[1] = 5;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
thr_procsub, // thread function name
vals, // argument to thread function
0, // use default creation flags
&thrsubId); // returns the thread identifier
if (hThread == NULL) throw;
DWORD res = WaitForSingleObject(hThread, 5000);
printf("result: %d", sub_res);
SuspendThread;
DWORD thrSumId;
//int vals[2];
vals[0] = 10;
vals[1] = 5;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
thr_procsum, // thread function name
vals, // argument to thread function
CREATE_SUSPENDED, // use default creation flags
&thrSumId); // returns the thread identifier
if (hThread == NULL) throw;
Sleep(5000);
ResumeThread(hThread);
//res = WaitForSingleObject(hThread, 5000);
printf("result: %d", sum_res);
Sleep(5000);
return 0;
}
#include <windows.h>
#include <stdio.h>
DWORD WINAPI thr_proc( LPVOID lpParameter )
{
//umn_res= ((int*)lpParameter)[0] * ((int*)lpParameter)[1];.
HANDLE mutex7 = CreateMutex(NULL, FALSE, L"MTX07");
HANDLE mutex8 = CreateMutex(NULL, FALSE, L"MTX08");
WaitForSingleObject(mutex7,INFINITE);
WaitForSingleObject(mutex8,INFINITE);
ReleaseMutex(mutex7);
ReleaseMutex(mutex8);
return 1;
}
//using namespace std;
int main(int argc, char** argv)
{
DWORD res;
// создаем объект-взаимоисключение
HANDLE mutex7 = CreateMutex(NULL, FALSE, L"MTX07");
// если он уже существует, CreateMutex вернет дескриптор существующего
// объекта, а GetLastError вернет ERROR_ALREADY_EXISTS
HANDLE mutex8 = CreateMutex(NULL, FALSE, L"MTX08");
// в течение 20 секунд пытаемся захватить объект
printf("Trying to get mutex...\n");
res = WaitForSingleObject(mutex7,20000);
DWORD thrSumId;
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
thr_proc, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&thrSumId); // returns the thread identifier
if (hThread == NULL) throw;
//INFINITE
if (res == WAIT_OBJECT_0) // если захват удался
{
int x=10;
int y=5;
int a=x*y;
int b=x/y;
// ждем 10 секунд
printf("Got it! Waiting for 10 secs...\n");
Sleep(10000);
// освобождаем объект
printf("Now releasing the object.\n");
ReleaseMutex(mutex7);
}
WaitForSingleObject(mutex8,INFINITE);
ReleaseMutex(mutex8);
// закрываем дескриптор
CloseHandle(mutex7);
CloseHandle(mutex8);
scanf("\n");
}
#include <windows.h>
#include <stdio.h>
DWORD WINAPI thr_proc( LPVOID lpParameter )
{
HANDLE event06 = CreateEvent(NULL, TRUE, TRUE, "APPNAME-EV01");
WaitForSingleObject(event06,INFINITE);
//ReleaseMutex(event06);
return 1;
}
int main(int argc, char** argv)
{
DWORD res;
// создаем объект-событие
HANDLE event06 = CreateEvent(NULL, FALSE, TRUE, "APPNAME-EV01");
if (event06 == NULL) throw;
// если он уже существует, то CreateEvent вернет дескриптор существующего
// объекта, а GetLastError вернет ERROR_ALREADY_EXISTS
printf("Trying to get event...\n");
// в течение 20 секунд пытаемся захватить объект
res = WaitForSingleObject(event06, 20000);
DWORD thrSumId;
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
thr_proc, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&thrSumId); // returns the thread identifier
if (hThread == NULL) throw;
DWORD thrId;
HANDLE sThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
thr_proc, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&thrSumId); // returns the thread identifier
if (hThread == NULL) throw;
if (res == WAIT_OBJECT_0) // если захват удался
{
int x=1;
x>>5;
printf("test %d \n", x);
// ждем 10 секунд
printf("Got it! Waiting for 10 secs...\n");
Sleep(10000);
// освобождаем объект
printf("Now releasing the object.\n");
//ReleaseMutex(event06);
PulseEvent( event06);
}
// закрываем дескриптор
CloseHandle(event06);
scanf("\n");
return 0;
}