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

DWORD WINAPI ThreadFun(LPVOID *lpParam)
{
	HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Synch");
	WaitForSingleObject(hMutex, INFINITE);

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

	ReleaseMutex(hMutex);
	return 0;
}

DWORD WINAPI ThreadFun2(LPVOID *lpParam)
{

	HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Synch");
	WaitForSingleObject(hMutex, INFINITE);

	int i = 0;

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

	ReleaseMutex(hMutex);
	return 0;
}

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

	HANDLE hMutex = CreateMutex(NULL, FALSE, "Synch");

	SetPriorityClass(GetCurrentProcess(), REALTIME_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);



	WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
	CloseHandle(hThread[0]);
	CloseHandle(hThread[1]);
}
