ThreadIf.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:5k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. #ifndef THREADIF_HXX
  2. #define THREADIF_HXX
  3. /* ====================================================================
  4.  * The Vovida Software License, Version 1.0 
  5.  * 
  6.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  7.  * 
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in
  17.  *    the documentation and/or other materials provided with the
  18.  *    distribution.
  19.  * 
  20.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  21.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  22.  *    not be used to endorse or promote products derived from this
  23.  *    software without prior written permission. For written
  24.  *    permission, please contact vocal@vovida.org.
  25.  *
  26.  * 4. Products derived from this software may not be called "VOCAL", nor
  27.  *    may "VOCAL" appear in their name, without prior written
  28.  *    permission of Vovida Networks, Inc.
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  31.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  33.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  34.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  35.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  36.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  37.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  41.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42.  * DAMAGE.
  43.  * 
  44.  * ====================================================================
  45.  * 
  46.  * This software consists of voluntary contributions made by Vovida
  47.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  48.  * Inc.  For more information on Vovida Networks, Inc., please see
  49.  * <http://www.vovida.org/>.
  50.  *
  51.  */
  52. #include <cassert>
  53. #include "VThread.hxx"
  54. #include "VMutex.h"
  55. static const char* const ThreadIf_hxx_version = 
  56. "$Id: ThreadIf.hxx,v 1.2 2001/06/29 03:41:33 bko Exp $";
  57. /*
  58. TODO:
  59. move constructor and operator= to private.  This cannot be done
  60. immediately since SipThread inherits from this and it has both a copy
  61. constructor and an operator=.
  62. */
  63. /** A wrapper class to create and spawn a thread.  It is a base class.
  64.  * ThreadIf::thread() is a pure virtual method .
  65.  *
  66.  * <P>Usage:
  67.  * To use this class, derive from it and override the thread() method.
  68.  * To start the thread, call the run() method.  The code in thread() will
  69.  * run in a separate thread.
  70.  *
  71.  * <P>Call shutdown() from the constructing thread to shut down the
  72.  * code.  This will set the bool shutdown_ to true.  The code in
  73.  * thread() should react properly to shutdown_ being set, by
  74.  * returning.  Call join() to join the code.
  75.  * <P>Sample:
  76.  * <PRE>
  77.  * ...
  78.  *     DerivedThreadIf thread;
  79.  *     thread.run();
  80.  * ... do stuff ...
  81.  *     thread.shutdown();
  82.  *     thread.join();
  83.  * </PRE>
  84.  */
  85. class ThreadIf
  86. {
  87.     public:
  88.         /// default constructor
  89.         ThreadIf();
  90.         /// destructor
  91.         virtual ~ThreadIf();
  92.         /// runs the code in thread() .  Returns immediately
  93.         virtual void run();
  94.         /// joins to the thread running thread()
  95.         virtual void join()
  96.         {
  97.             itsThread.join();
  98.         }
  99.         /// forces the thread running to exit()
  100.         virtual void exit()
  101.         {
  102.             itsThread.exit();
  103.         }
  104. /** request the thread running thread() to return, by setting
  105.  * the bool shutdown_ 
  106.  */
  107.         void shutdown();
  108. /** returns true if the thread has been asked to shutdown_ or
  109.  * if the thread is not running
  110.  */
  111.         bool isShutdown() const;
  112.         /** thread is a virtual method.  Users should derive and define
  113.  * thread() such that it returns when isShutdown() is true.
  114.  */
  115.         virtual void thread() = 0;
  116.     protected:
  117. /// itsThread is protected so that thread() can retrieve its threadId 
  118.         VThread itsThread;
  119.     private:
  120.         bool shutdown_;
  121.         mutable VMutex shutdownMutex_;
  122.         /** Suppress copying
  123.          */
  124.         ThreadIf(const ThreadIf &);
  125.         /** Suppress copying
  126.          */
  127.         const ThreadIf & operator=(const ThreadIf &);
  128. };
  129. // THREADIF_HXX
  130. #endif