// vlakna2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" int a = 100; int b = 200; HANDLE sem = NULL; DWORD WINAPI prevod (void * param) { while (true) { WaitForSingleObject (sem, INFINITE); if (a > 20) { a -= 20; b += 20; } else { b -= 10; a += 10; } ReleaseSemaphore (sem, 1, NULL); } return 0; } DWORD WINAPI soucet (void * param) { while (true) { WaitForSingleObject (sem, INFINITE); int s = a + b; ReleaseSemaphore (sem, 1, NULL); if (s != 300) printf ("Soucet je spatny %d \n", s); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { sem = CreateSemaphore (NULL, // security attributes 1, // InitialCount 1, // MaximumCount NULL); // name if (sem == NULL) return 1; for (int i = 1; i <= 3; i++) CreateThread (NULL, // security attributes 0, // stack size prevod, NULL, // parameter 0, // flags NULL ); CreateThread (NULL, // security attributes 0, // stack size soucet, NULL, // parameter 0, // flags NULL ); printf ("Stisknete enter \n"); char c; scanf ("%c", &c); return 0; }