Guest User

Untitled

a guest
Jul 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. // task 1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <Windows.h>
  7. using namespace std;
  8. const int N=15;
  9.  
  10. double Array[N];
  11.  
  12. HANDLE hEvent;
  13.  
  14. DWORD WINAPI exchange(LPVOID param)
  15. {
  16. double fict;
  17. WaitForSingleObject(hEvent,INFINITE);
  18. for (int i=1;i<N;i++)
  19. {
  20. if (Array[i-1]>Array[i])
  21. {
  22. fict=Array[i];
  23. Array[i]=Array[i-1];
  24. Array[i-1]=fict;
  25. }
  26. if (i==(N/2))
  27. {
  28. SetEvent(hEvent);
  29. }
  30. }
  31. return 0;
  32. }
  33.  
  34.  
  35. int _tmain(int argc, _TCHAR* argv[])
  36. {
  37. int i;
  38. HANDLE hThread[N-1];
  39. DWORD dwThreadID[N-1];
  40. cout<<"It was:"<<endl;
  41. for (i=0;i<N;i++)
  42. {
  43. Array[i]=rand()%1000;
  44. cout<<Array[i]<<" ";
  45. }
  46. cout<<endl;
  47. hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
  48. for (i=0;i<N-1;i++)
  49. {
  50. hThread[i]=CreateThread(NULL,0,exchange,NULL,0,&dwThreadID[i]);
  51. }
  52. WaitForMultipleObjects(N-1,hThread,true,INFINITE);
  53. cout<<"Now it is:"<<endl;
  54. for (i=0;i<N;i++)
  55. {
  56. cout<<Array[i]<<" ";
  57. }
  58. cout<<endl;
  59. system("pause");
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment