Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <atomic>
  3. #include <mutex>
  4. #include <thread>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <tchar.h>
  9. #include <iostream>
  10. #include <list>
  11. #include <chrono>
  12. #include <stdlib.h>     /* srand, rand */
  13. #include <time.h>
  14. #include <vector>
  15.  
  16. using namespace std;
  17. CRITICAL_SECTION csS;
  18. CRITICAL_SECTION csR;
  19.  
  20. void threadA(list<int> &S)
  21. {
  22.     int i = 0;
  23.  
  24.     while(1)
  25.     {
  26.         EnterCriticalSection(&csS);
  27.         S.push_back(i++);
  28.         cout << S.back();
  29.         LeaveCriticalSection(&csS);
  30.     }
  31. }
  32.  
  33. void threadB(list<int> &S,list<double> &R)
  34. {
  35.     while(1)
  36.     {
  37.         EnterCriticalSection(&csS);
  38.         if (!S.empty())
  39.         {
  40.             double a = S.back()*S.back();
  41.             EnterCriticalSection(&csR);
  42.             R.push_back(a);
  43.             LeaveCriticalSection(&csR);
  44.         }
  45.         else
  46.             Sleep(1000);
  47.         LeaveCriticalSection(&csS);
  48.     }
  49. }
  50.  
  51. void threadC(list<int> &S,list<double> &R)
  52. {
  53.     while(1)
  54.     {
  55.         EnterCriticalSection(&csS);
  56.         if (!S.empty())
  57.         {
  58.             double a = S.back()/3;
  59.             EnterCriticalSection(&csR);
  60.             R.push_back(a);
  61.             LeaveCriticalSection(&csR);
  62.         }
  63.         else
  64.             Sleep(1000);
  65.         LeaveCriticalSection(&csS);
  66.     }
  67. }
  68.  
  69. void threadD(list<double> &R)
  70. {
  71.     while(1)
  72.     {
  73.         EnterCriticalSection(&csR);
  74.         if (!R.empty())
  75.         {
  76.             cout << R.back() << endl;
  77.         }
  78.         else
  79.         {
  80.             cout << "R is empty\n";
  81.             Sleep(1000);
  82.         }
  83.         LeaveCriticalSection(&csR);
  84.     }
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90.     list<int> S;
  91.     list<int> R;
  92.     HANDLE hThr[4];
  93.     unsigned long uThrID;
  94.  
  95.     DWORD dwEvent;
  96.  
  97.     InitializeCriticalSection(&csS);
  98.  
  99.     InitializeCriticalSection(&csR);
  100.  
  101.  
  102.     hThr[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadA,NULL,0,&uThrID);
  103.     cout << "A\n";
  104.     hThr[1] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadB,NULL,0,&uThrID);
  105.     cout << "B\n";
  106.     hThr[2] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadC,NULL,0,&uThrID);
  107.     cout << "C\n";
  108.     hThr[3] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadD,NULL,0,&uThrID);
  109.     cout << "D\n";
  110.  
  111.     dwEvent = WaitForMultipleObjects(4, hThr, true, INFINITE);
  112.  
  113.     cout << "Hello world!" << endl;
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement