Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2012
2,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <thread>
  2. #include <chrono>
  3. #include <iostream>
  4. #include <mutex>
  5. #include <list>
  6. #include <limits>
  7.  
  8. class WarehouseEmpty
  9. {
  10. };
  11.  
  12. unsigned short c_SpecialItem = std::numeric_limits<unsigned short>::max();
  13.  
  14. class Warehouse
  15. {
  16.     std::list<unsigned short> m_Store;
  17. public:
  18.     void AcceptItem(unsigned short item)
  19.     {
  20.         m_Store.push_back(item);
  21.     }
  22.     unsigned short HandLastItem()
  23.     {
  24.         if(m_Store.empty())
  25.             throw WarehouseEmpty();
  26.         unsigned short item = m_Store.front();
  27.         if(item != c_SpecialItem)
  28.             m_Store.pop_front();
  29.         return item;
  30.     }
  31. };
  32.  
  33. Warehouse g_FirstWarehouse;
  34. Warehouse g_SecondWarehouse;
  35. std::timed_mutex g_FirstMutex;
  36. std::mutex g_SecondMutex;
  37.  
  38. int main()
  39. {
  40.     auto suplier = []()
  41.     {
  42.         for(unsigned short i = 0, j = 0; i < 10 && j < 10;)
  43.         {
  44.             if(i < 100 && g_FirstMutex.try_lock())
  45.             {
  46.                 g_FirstWarehouse.AcceptItem(i);
  47.                 i++;
  48.                 g_FirstMutex.unlock();
  49.             }
  50.             if(j < 100 && g_SecondMutex.try_lock())
  51.             {
  52.                 g_SecondWarehouse.AcceptItem(j);
  53.                 j++;
  54.                 g_SecondMutex.unlock();
  55.             }
  56.             std::this_thread::yield();
  57.         }
  58.         g_FirstMutex.lock();
  59.         g_SecondMutex.lock();
  60.         g_FirstWarehouse.AcceptItem(c_SpecialItem);
  61.         g_SecondWarehouse.AcceptItem(c_SpecialItem);
  62.         g_FirstMutex.unlock();
  63.         g_SecondMutex.unlock();
  64.     };
  65.     auto consumer = []()
  66.     {
  67.         while(true)
  68.         {
  69.             g_FirstMutex.lock();
  70.             unsigned short item = 0;
  71.             try
  72.             {
  73.                 item = g_FirstWarehouse.HandLastItem();
  74.             }
  75.             catch(Warehouse)
  76.             {
  77.                 std::cout << "Warehouse is empty!\n";
  78.             }
  79.             g_FirstMutex.unlock();
  80.             if(item == c_SpecialItem)
  81.                 break;
  82.             std::cout << "Got new item: " << item << "!\n";
  83.             std::this_thread::sleep_for(std::chrono::seconds(4));
  84.         }
  85.     };
  86.     auto impatientConsumer = []()
  87.     {
  88.         while(true)
  89.         {
  90.             unsigned short item = 0;
  91.             if(g_FirstMutex.try_lock_for(std::chrono::seconds(2)))
  92.             {
  93.                 try
  94.                 {
  95.                     item = g_FirstWarehouse.HandLastItem();
  96.                 }
  97.                 catch(Warehouse)
  98.                 {
  99.                     std::cout << "Warehouse is empty! I'm mad!!!11\n";
  100.                 }
  101.                 g_FirstMutex.unlock();
  102.             }
  103.             else
  104.             {
  105.                 std::cout << "First warehouse always busy!!!\n";
  106.                 g_SecondMutex.lock();
  107.                 try
  108.                 {
  109.                     item = g_SecondWarehouse.HandLastItem();
  110.                 }
  111.                 catch(Warehouse)
  112.                 {
  113.                     std::cout << "2nd warehouse is empty!!!!11\n";
  114.                 }
  115.                 g_SecondMutex.unlock();
  116.             }
  117.             if(item == c_SpecialItem)
  118.                 break;
  119.             std::cout << "At last I got new item: " << item << "!\n";
  120.             std::this_thread::sleep_for(std::chrono::seconds(4));
  121.         }
  122.     };
  123.  
  124.     std::thread supplierThread(suplier);
  125.     std::thread consumerThread(consumer);
  126.     std::thread impatientConsumerThread(impatientConsumer);
  127.  
  128.     supplierThread.join();
  129.     consumerThread.join();
  130.     impatientConsumerThread.join();
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement