Advertisement
zhangsongcui

ThreadPool using boost::thread

Sep 18th, 2011
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/thread.hpp>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. boost::condition_variable condDoTask, condOutput;
  9. boost::mutex globalMutex;
  10.  
  11. volatile bool isMissionComplete = false;
  12.  
  13. void Encode(std::queue<std::pair<unsigned, std::string> >& in, std::map<unsigned, std::string>& out)
  14. {
  15.     boost::mutex localMutex;
  16.     while (true)
  17.     {
  18.         boost::mutex::scoped_lock lock(localMutex);
  19.         condDoTask.wait(lock, [&in] { return !in.empty() || isMissionComplete; });
  20.         if (isMissionComplete)
  21.             break;
  22.         globalMutex.lock();
  23.         std::pair<unsigned, std::string> deal(std::move(in.front()));
  24.         globalMutex.unlock();
  25.         in.pop();
  26.         ///////// DO YOUR TASK HERE //////////
  27.         std::for_each(deal.second.begin(), deal.second.end(),
  28.             [] (char& c) { c = std::isupper(c) ? std::tolower(c) : std::toupper(c); } );
  29.         ///////// DO YOUR TASK HERE //////////
  30.         globalMutex.lock();
  31.         out.insert(std::move(deal));
  32.         globalMutex.unlock();
  33.         condOutput.notify_all();
  34.     }
  35.     condOutput.notify_all();
  36. }
  37.  
  38. void Output(std::map<unsigned, std::string>& out, const volatile unsigned& total)
  39. {
  40.     unsigned count = 0;
  41.     boost::mutex localMutex;
  42.     while (true)
  43.     {
  44.         boost::mutex::scoped_lock lock(localMutex);
  45.         condOutput.wait(lock,
  46.             [&out, count, &total] { return (!out.empty() && count == out.begin()->first) || ( isMissionComplete && count == total); });
  47.         if (isMissionComplete && count == total)
  48.             break;
  49.         std::cout << out.begin()->second << std::endl;
  50.         globalMutex.lock();
  51.         out.erase(out.begin());
  52.         globalMutex.unlock();
  53.         ++count;
  54.     }
  55. }
  56.  
  57. int main()
  58. {
  59.     enum { NumOfThread = 2 };
  60.  
  61.     boost::thread_group threads;
  62.     std::queue<std::pair<unsigned, std::string> > in;
  63.     std::map<unsigned, std::string> out;
  64.     volatile unsigned count = 0;
  65.     boost::thread outputThread(Output, std::ref(out), std::ref(count));
  66.  
  67.     for (int i=0; i!=NumOfThread; ++i)
  68.         threads.create_thread(std::bind(Encode, std::ref(in), std::ref(out)));
  69.    
  70.     std::string str;
  71.     while (std::cin >> str)
  72.     {
  73.         in.push(make_pair(count++, std::move(str)));
  74.         condDoTask.notify_one();
  75.     }
  76.     isMissionComplete = true;
  77.     condDoTask.notify_all();
  78.  
  79.     outputThread.join();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement