#include "pch.h"
#include <iostream>
#include <Windows.h>
using namespace std;

DWORD WINAPI ThreadFun(LPVOID *lpParam)
{
	HANDLE hEvent = OpenEvent(SYNCHRONIZE, FALSE, "CAN_PRINT1");
	WaitForSingleObject(hEvent, INFINITE);

	int i = 0;
	while (i < 100)
	{
		i++;
		cout << "Licznik inkrementacji watku 1 wynosi: " << i << endl;
	}


	return 0;
}

DWORD WINAPI ThreadFun2(LPVOID *lpParam)
{
	HANDLE hEvent = OpenEvent(SYNCHRONIZE, FALSE, "CAN_PRINT2");
	WaitForSingleObject(hEvent, INFINITE);


	int i = 0;

	while (i < 100)
	{
		i++;
		cout << "Licznik inkrementacji watku 2 wynosi: " << i << endl;
	}


	return 0;
}

int main()
{
	HANDLE hThread[2];
	DWORD ThreadID1;

	DWORD ThreadID2;

	HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, "CAN_PRINT1");
	HANDLE hEvent1 = CreateEvent(NULL, TRUE, FALSE, "CAN_PRINT2");

	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
	SetThreadPriority(GetCurrentThread(), -15);
	hThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFun, NULL, 0, &ThreadID1);
	hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFun2, NULL, 0, &ThreadID2);


	SetThreadPriority(hThread[0], -15);
	SetThreadPriority(hThread[1], 15);


	SetEvent(hEvent);
	WaitForSingleObject(hThread[0], INFINITE);

	SetEvent(hEvent1);
	WaitForSingleObject(hThread[1], INFINITE);


	CloseHandle(hThread[0]);
	CloseHandle(hThread[1]);
}
