Advertisement
andruhovski

Untitled

Sep 28th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // os-p0103.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <process.h>
  7. int d = 0;
  8. //void _cdecl function(void*);
  9. unsigned _stdcall function(void*);
  10. CRITICAL_SECTION CriticalSection;
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13. int x=1,y=2,z=3;
  14. unsigned int id[3];
  15. HANDLE hd[3];
  16.  
  17. if (!InitializeCriticalSectionAndSpinCount(&CriticalSection,
  18. 0x00000400))
  19. return 0;
  20.  
  21. hd[0] = (HANDLE)_beginthreadex(NULL, 0, &function, &x, 0, &id[0]);
  22. //EnterCriticalSection(&CriticalSection);
  23. hd[1] = (HANDLE)_beginthreadex(NULL, 0, &function, &y, 0, &id[1]);
  24. //EnterCriticalSection(&CriticalSection);
  25. hd[2] = (HANDLE)_beginthreadex(NULL, 0, &function, &z, 0, &id[2]);
  26. //LeaveCriticalSection(&CriticalSection);
  27.  
  28. WaitForMultipleObjects(3,hd, TRUE, INFINITE);
  29. DeleteCriticalSection(&CriticalSection);
  30. return 0;
  31. }
  32.  
  33. unsigned _stdcall function(void* k)
  34. {
  35. int* k1 = (int*)k;
  36. for (size_t i = 0; i < 10;)
  37. {
  38. EnterCriticalSection(&CriticalSection);
  39. if (*k1 == (d+1)){
  40. if (*k1<3)
  41. printf_s("%d", *k1);
  42. else
  43. {
  44. printf_s("%d\n", *k1);
  45. }
  46. d=(d+1)%3;
  47. i++;
  48. }
  49. LeaveCriticalSection(&CriticalSection);
  50. Sleep(200);
  51. }
  52. _endthread();
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement