Possible language changes

Douglas C. Schmidt schmidt at dre.vanderbilt.edu
Thu Mar 3 16:29:07 GMT 2005


Hi folks,

> Plea for help: Doug Schmidt and/or others. Could you sketch out a
> candidate Thread class?

Sure, there are two common approaches I see:

. A reasonably sophisticated object-oriented thread class, e.g., class
  Thread and ThreadGroup in Java.  I assume everyone knows more or less
  what this would look like, so I've omitted it here.  There's an
  example in ACE called "ACE_Task" that illustrates the design of
  something a bit more powerful than Java Thread/ThreadGroup (ACE_Task
  provides a queue and can manage a pool of threads, which makes it easy
  to implement variants of the Active Object pattern).  Please see

  www.dre.vanderbilt.edu/Doxygen/Current/html/ace/classACE__Task.html
  
  for the interface.  If I was doing this from scratch (i.e., >10 years
  later) I can think of a LOT of improvements that would bring this
  stuff into the 21st century ;-)

. A "barebones" set of portable wrapper facades around common OS
  threading capabilities.  In this approach, the caller would pass in a
  pointer to a function that would be the entry point in a new thread.
  I've enclosed a sketch of this approach below, differentiating between
  operations that could be implemented portably vs. those that aren't
  portable.
  
We provide both of these approaches in ACE since they appeal to
different sorts of users.

Take care,

     Doug

----------------------------------------

/**
 * @class Thread
 *
 * @brief Spawn threads and wait for them to exit.
 */

class Thread
{
public:
  /// The following are methods that can be implemented portably on all
  /// (civilized) platforms.

  /**
   * Create a new thread, which executes @a func with argument @a arg.
   * @a thread_parameters can be used to pass attributes that control
   * properties of the thread, such as priority, stack size, stack memory,
   * detached/joinable flags, etc.
   * Returns @a thread_t id that uniquely identifes the thread.
   * Can throw various exceptions on failure (TBD).
   */
   thread_t spawn (void * (*func) (void *),
                   void *arg = 0,
                   Thread_Params *thread_parameters = 0);
             
  /**
   * Wait for a thread specified by @a thread_id to exit and
   * optionally returns its @a exit_status.  Does not wait on detached
   * threads. 
   * Can throw various exceptions on failure (TBD).
   */
  void wait (thread_t thread_id, void *exit_status = 0);

  /// The following are methods that cannot be implemented portably as
  /// a library, but are available on many operating systems.

  /**
   * Send @a signum to @a thread_id.  Not supported on platforms
   * that do not have advanced signal support, such as Win32.
   * Can throw various exceptions on failure (TBD).
   */
  void signal (thread_t thread_id,
               int signum);

  /**
   * Cancel a single thread.  Not supported on platforms
   * that do not have advanced cancellation support.
   * Can throw various exceptions on failure (TBD).
   */
  void cancel (thread_t, bool async_cancel = false);

  /**
   * True if @a thread_id is cancelled, else false.
   */
  bool testcancel (thread_t thread_id);

  /// I've intentionally omitted all suspend()/resume() methods
  /// since they are badly flawed, non-portable, and can't be used
  /// properly.
};

/**
 * @class Thread_Group
 *
 * @brief Manages a group of threads.
 *
 * Allows operations on groups of threads atomically.
 */

class Thread_Group
{
  /**
   * Add @a thread_id to the group.
   * Can throw various exceptions on failure (TBD).
   */
  void join (thread_t thread_id);

  /**
   * Remove @a thread_id from the group.
   * Can throw various exceptions on failure (TBD).
   */
  void remove (thread_t thread_id);

  /**
   * Perform a "barrier" operation that blocks until there are no more
   * threads running in the @a Thread_Group or the @a timeout expires.
   * The @a timeout is treated as "absolute" time.  If @a
   * abandon_detached_threads is set, wait will first check thru its
   * thread list for threads with the @a THR_DETACHED flag set and
   * remove these threads. 
   * Can throw various exceptions on failure (TBD).
   */
  int wait (const timeval *timeout = 0,
            int abandon_detached_threads = 0);

  /**
   * Returns a vector @a thread_ids that are in the @a Thread_Group.
   * Can throw various exceptions on failure (TBD).
   */
  vector<thread_it> list ();

  /// The following "group" methods cannot be implemented portably as
  /// a library, but are available on many operating systems.

  /**
   * Send @a signum to all threads in the group.  Not supported
   * on platforms that do not have advanced signal support, such as Win32.
   * Can throw various exceptions on failure (TBD).
   */
  void signal (int signum);

  /**
   * Cancel all threads in the group.  Not supported on platforms
   * that do not have advanced cancellation support.
   * Can throw various exceptions on failure (TBD).
   */
  void cancel (thread_t, bool async_cancel = false);

  /// I've intentionally omitted all suspend()/resume() methods
  /// since they are badly flawed, non-portable, and can't be used
  /// properly.
};






More information about the cpp-threads mailing list