Author: James CE Johnson

ACE Tutorial 010
Passing chunks of data through an ACE_Message_Queue

Our Task object executes in one or more threads and reads from the message queue it contains.

    // page04.html,v 1.8 1999/04/03 21:56:27 jcej Exp

    #ifndef TASK_H
    #define TASK_H

    #include "ace/Task.h"

    #if !defined (ACE_LACKS_PRAGMA_ONCE)
    # pragma once
    #endif /* ACE_LACKS_PRAGMA_ONCE */

    /* Like the thread-pool server tutorial, we'll derive from ACE_Task<>.
      Our goal here is to show off the ACE_Message_Queue and the best way
      to do that is to use one to pass data between threads.  The easiest
      way to create threads is with ACE_Task<> */
    class Task : public ACE_Task <ACE_MT_SYNCH>
    {
    public:

      typedef ACE_Task <ACE_MT_SYNCH> inherited;

      /* The constructor/destructor are simple but take care of some
        necessary housekeeping.  */
      Task (size_t n_threads);
      ~Task (void);

      /* open() will kick off our thread pool for us.  */
      int open (void * = 0);

      /* Our worker method */
      int svc (void);

      /* All we'll do here is print a message to the user.  */
      int close (u_long flags = 0);

    protected:
      /* Just to be clever, I'll use an ACE_Barrier to cause the threads
        to sync in svc() before doing any real work.  */
      ACE_Barrier barrier_;

      size_t n_threads_;
      // Number of threads in the pool.
    };

    #endif /* TASK_H */

The only thing here that we didn't see in the thread-pool server is the ACE_Barrier. The application logic really doesn't need it but it is a handy way to synchronize the threads at the beginning of svc(). In testing I found that if I didn't sync svc(), the first thread to get activated would tend to get all of the messages before the other threads came alive.

[Tutorial Index] [Continue This Tutorial]