Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1.  
  2. #include <Windows.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <stdio.h>
  6.  
  7. CRITICAL_SECTION criticalSection;
  8. long int zmienna = 1;
  9.  
  10.  
  11.     unsigned long noOfEntrance1;
  12.     unsigned long noOfEntrance2;
  13.     unsigned long CompletedEntrance = 0;
  14.     unsigned long AbortiveEntrance = 0;
  15.  
  16. unsigned long __stdcall thread (long* v);
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.  
  21.     InitializeCriticalSection(&criticalSection);
  22.  
  23.     HANDLE hw1;
  24.     hw1 = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread, &noOfEntrance1, 0, 0);
  25.     HANDLE hw2;
  26.     hw2 = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread, &noOfEntrance2, 0, 0);
  27.     while(1)
  28.     {
  29.         printf("%ld\t%ld\t%ld\t%ld\t%ld\n", zmienna, noOfEntrance1, noOfEntrance2, CompletedEntrance, AbortiveEntrance);
  30.         Sleep(200);
  31.     }
  32.     CloseHandle(hw1);
  33.     CloseHandle(hw2);
  34.     DeleteCriticalSection(&criticalSection);
  35.     return 0;
  36. }
  37.  
  38. unsigned long __stdcall thread (long* v)
  39. {
  40.     //EnterCriticalSection(&criticalSection);
  41.     while(1)
  42.     {
  43.         //EnterCriticalSection(&criticalSection);
  44.         //EnterCriticalSection(&criticalSection);
  45.         if(TryEnterCriticalSection(&criticalSection))
  46.         {
  47.         zmienna += 100;
  48.         zmienna *= 200;
  49.         zmienna /= 200;
  50.         zmienna -= 100;
  51.         (*v)++;
  52.         CompletedEntrance++;
  53.         LeaveCriticalSection(&criticalSection);
  54.         }
  55.         else
  56.         {
  57.             AbortiveEntrance++;
  58.             Sleep(20);
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement