Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. template < class T >
  5. class Singleton
  6. {
  7. protected:
  8.     Singleton( void ) = default;
  9.     Singleton( const Singleton& other ) = delete;
  10.     Singleton( Singleton&& other ) = delete;
  11.  
  12. public:
  13.     static T* Instance( void )
  14.     {
  15.         static T instance;
  16.         return &instance;
  17.     }
  18. };
  19.  
  20. class Test : public Singleton< Test >
  21. {
  22.     friend Test* Singleton< Test >::Instance( void );
  23.  
  24.     Test( void )
  25.     {
  26.         printf( "Test()\n" );
  27.     }
  28.  
  29. public:
  30.     void run( int i )
  31.     {
  32.         printf( "Thread %i run( int )\n", i );
  33.     }
  34. };
  35.  
  36. void Thread_Func( int idx )
  37. {
  38.     Test::Instance()->run( idx );
  39.     Singleton< Test >::Instance()->run( idx );
  40. }
  41.  
  42. int main( void )
  43. {
  44.     std::thread t_1( Thread_Func, 1 );
  45.     std::thread t_2( Thread_Func, 2 );
  46.     std::thread t_3( Thread_Func, 3 );
  47.     std::thread t_4( Thread_Func, 4 );
  48.     std::thread t_5( Thread_Func, 5 );
  49.  
  50.     t_1.detach();
  51.     t_2.detach();
  52.     t_3.detach();
  53.     t_4.detach();
  54.     t_5.detach();
  55.  
  56.     std::cin.get();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement