Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Synchronous reading from IOBuffer
  2. void thread1_func(ostream& os)
  3. {
  4.     sleep(10);
  5.     os << 12;
  6.     sleep(10);
  7.     os << "text";
  8.     sleep(10);
  9.     os << MyObject();
  10. }
  11.  
  12. void thread2_func(istream& is)
  13. {
  14.     int i = 0;
  15.     std::string str;
  16.     MyObject obj;
  17.  
  18.     is >> 12;    // waits until 12 is written
  19.     is >> str;   // waits for "text"
  20.     is >> obj;   // waits for MyObject
  21. }
  22.  
  23.  
  24. void main()
  25. {
  26.     IOBuffer buf;
  27.  
  28.     // create two threads:
  29.     thread1_func(ostream(buf));
  30.     thread2_func(istream(buf));
  31.  
  32. }
  33.        
  34. void main()
  35. {
  36.     boost::asio::streambuf buf;
  37.     std::ostream os(&buf);
  38.     std::istream is(&buf);
  39.     //...
  40. }