Advertisement
Guest User

so

a guest
Dec 9th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <iostream.h>
  6. #include <conio.h>
  7.  
  8. //---------------------------------------------------------------------------
  9.  
  10. #pragma argsused
  11.  
  12. HANDLE h1,h2;
  13. int globalCounter=0;
  14. VOID InitializeCriticalSection
  15. CRITICAL_SECTION sectiune_critica;
  16.  
  17. DWORD WINAPI Increment_Contor1(){
  18. while(true){
  19. EnterCriticalSection(&sectiune_critica);
  20. for (int i=0; i<10; i++)
  21. {
  22. globalCounter++;
  23. printf("Thread1");
  24. printf("%d",i);
  25. printf("global");
  26. printf("%d", globalCounter);
  27. printf("\n");
  28.  
  29. }
  30. LeaveCriticalSection(&sectiune_critica);
  31. }
  32. }
  33.  
  34. DWORD WINAPI Increment_Contor2(){
  35. while(true){
  36. EnterCriticalSection(&sectiune_critica);
  37. for (int i=0; i<10; i++)
  38. {
  39.  
  40. globalCounter++;
  41. printf("Thread2");
  42. printf("%d",i);
  43. printf("global");
  44. printf("%d", globalCounter);
  45. printf("\n");
  46.  
  47. }
  48. LeaveCriticalSection(&sectiune_critica);
  49. }
  50. }
  51.  
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55. h1=CreateThread(NULL,0,(PTHREAD_START_ROUTINE)Increment_Contor1,NULL,0,NULL);
  56. h2=CreateThread(NULL,0,(PTHREAD_START_ROUTINE)Increment_Contor2,NULL,0,NULL);
  57. getch();
  58.  
  59. TerminateThread(h1,0);
  60. TerminateThread(h2,0);
  61.  
  62. return 0;
  63. }
  64. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement