#include "stdafx.h" #include "windows.h" int a = 100; int b = 200; HANDLE S = NULL; DWORD WINAPI prevod (void * param) { while (true) { WaitForSingleObject (S, INFINITE); if (a > 20) { a -= 20; b += 20; }else{ a += 20; b -= 20; } ReleaseSemaphore (S, 1, NULL); } return 0; } DWORD WINAPI kontrola (void * param) { while (true) { WaitForSingleObject (S, INFINITE); int s = a + b; ReleaseSemaphore (S, 1, NULL); if (s != 300) printf ("Soucet je %d \n", s); // if (a+b != 300) // printf ("Soucet je %d \n", a+b); } } int _tmain(int argc, _TCHAR* argv[]) { S = CreateSemaphore (NULL, // lpSemaphoreAttributes 1, // lInitialCount 1, // lMaximumCount NULL); // name CreateThread (NULL, // lpThreadAttributes 0, // dwStackSize, prevod, NULL, // parameter 0, // flags NULL); // thread id CreateThread (NULL, // lpThreadAttributes 0, // dwStackSize, kontrola, NULL, // parameter 0, // flags NULL); // thread id char c; printf ("Stiskni enter \n"); scanf ("%c", &c); return 0; }