thread pool

thread pool - C++ - Programmation

Marsh Posté le 27-03-2005 à 10:32:51    

Bonjour,
 
 Je cherche des informations sur les architectures de type thread pool en C++. Pouvez vous m'indiquer un site qui explique ou détaille une telle implémentation. Est-il préférable d'exploiter un framework orienté réseau (.Net, ACE et Cie) ?
 
Merci.

Reply

Marsh Posté le 27-03-2005 à 10:32:51   

Reply

Marsh Posté le 27-03-2005 à 10:40:24    

http://forum.hardware.fr/hardwaref [...] 6556-1.htm
[:dawa]
 
Un ThreadPool, c'est bêtement une collection (une liste, un tableau, etc.) de Threads qui sont prêtes à exécuter les tâches que tu leur fournis.
 
Ca marche très bien si tu fais faire le dispatch de ton boulot par une "Command Queue".

Reply

Marsh Posté le 27-03-2005 à 10:56:47    

Je souhaiterais me baser sur boost::thread. Je peux certainement m'insiprer de PACC pour faire une sur couche au dessus de boost pour gérer le pool.

Reply

Marsh Posté le 28-03-2005 à 19:00:24    

as-tu réussi à utiliser la librairie PACC pour les threads ?
C'est-à-dire à la compiler pour qu'elle soit utilisée comme une librairie dans vs c++ .net?

Reply

Marsh Posté le 31-03-2005 à 19:36:56    

Je tenais à coder ça par moi même, et bien le resultat est pas triste bien sur. Assert dans Boost::mutex.inl. toute suggestion est la bienvenue.
 

Code :
  1. template< typename task, typename function >
  2. struct thread_pool : private boost::noncopyable
  3. {
  4. // one thread of the pool
  5. struct one_thread
  6. {
  7.  // constructor
  8.  one_thread( bool & r, fifo< task >& f ) : running( r ), task_queue( f ) {};
  9.  // thread function
  10.  void operator()() const
  11.  {
  12.   while( running )
  13.    function()( task_queue.read() );
  14.  }
  15. private:
  16.  bool& running;
  17.  fifo< task >& task_queue;
  18. };
  19. // constructor
  20. thread_pool( unsigned const & thread_number ) :
  21. task_queue( thread_number * 2 ),
  22. running( true )
  23. {
  24.  // create all the threads of the pool
  25.  for( unsigned index = 0; index != thread_number; ++index )
  26.   tg.create_thread( one_thread( running, task_queue ) );
  27. }
  28. // new task
  29. inline void push_task( task const & t )
  30. {
  31.  task_queue.write( t );
  32. }
  33. // stop
  34. inline void stop()
  35. {
  36.  running = false;
  37. }
  38. // destructor
  39. ~thread_pool()
  40. {
  41.  running = false;
  42.  tg.join_all();
  43. }
  44. private:
  45. // thread group
  46. thread_group tg;
  47. // message queue
  48. fifo< task > task_queue;
  49. // status
  50. bool running;
  51. };


 
Au cas ou, voici le code de la fifo (qui marche parfaitement).
 

Code :
  1. template< typename X >
  2. struct fifo : private boost::noncopyable
  3. {
  4.     fifo( const int& n ) :
  5. begin( 0 ),
  6. end( 0 ),
  7. buffered( 0 ),
  8. circular_buf( n )
  9. {
  10. }
  11.     void write( const X& m )
  12. {
  13.         lock lk( monitor );
  14.         while ( buffered == circular_buf.size() )
  15.             buffer_not_full.wait( lk );
  16.         circular_buf[ end ] = m;
  17.         end = ( end + 1 ) % circular_buf.size();
  18.         ++buffered;
  19.         buffer_not_empty.notify_one();
  20.     }
  21.     X read()
  22. {
  23.  lock lk( monitor );
  24.  while ( buffered == 0 )
  25.   buffer_not_empty.wait( lk );
  26.  X i = circular_buf[ begin ];
  27.  begin = ( begin + 1 ) % circular_buf.size();
  28.  --buffered;
  29.         buffer_not_full.notify_one();
  30.         return i;
  31.     }
  32. private:
  33.     unsigned int begin, end, buffered;
  34.     vector< X > circular_buf;
  35.     condition buffer_not_full, buffer_not_empty;
  36. mutex monitor;
  37. };


---------------
Cordialement, Xterm-in'Hate...
Reply

Marsh Posté le 31-03-2005 à 21:14:21    

boost::thread_group est buggé dans mon environnement de développement. C'est bien la première fois qu'un composant boost foire...


---------------
Cordialement, Xterm-in'Hate...
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed