Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 23rd, 2012  |  syntax: C++  |  size: 0.54 KB  |  views: 37  |  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. #include <iostream>
  2. #include <boost/thread.hpp>
  3. using namespace std;
  4.  
  5. struct A
  6. {
  7.   void operator()()
  8.   {
  9.     sleep(1);
  10.     cout << "Hello from thread" << endl;
  11.   }
  12.  
  13.   boost::thread t;
  14.  
  15.   void start()
  16.   {
  17.     t = boost::thread(boost::ref(*this));
  18.   }
  19. };
  20.  
  21. struct Controller : public A
  22. {
  23. };
  24.  
  25. int main()
  26. {
  27.   Controller controller;
  28.   controller.start();
  29.   cout << "Thread started" << endl;
  30.   controller.t.join();
  31. }
  32.  
  33.  
  34. //  $ g++ test.cpp -lboost_thread -pthread
  35. //  $ ./a.out
  36. //  Thread started
  37. //  Hello from thread
  38. //  $