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

CRITICAL_SECTION cs; 

DWORD WINAPI ThreadFun(LPVOID *lpParam)
{

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

	LeaveCriticalSection(&cs);
	return 0;
}

DWORD WINAPI ThreadFun2(LPVOID *lpParam)
{
	EnterCriticalSection(&cs);

	int i = 0;

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

	LeaveCriticalSection(&cs);
	return 0;
}

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

	DWORD ThreadID2;

	InitializeCriticalSection(&cs);

	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);

	EnterCriticalSection(&cs);

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

	LeaveCriticalSection(&cs);

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