yate学习--yateclass.h--class YATE_API Thread : public Runnable

    请声明出处:http://blog.csdn.net/u012377333/article/details/45392379

    yate的线程类:

/**
 * A thread is a separate execution context that exists in the same address
 *  space. Threads make better use of multiple processor machines and allow
 *  blocking one execution thread while allowing other to run.
 * 一个线程是一个独立的执行上下文,
 * 存在于相同的地址空间。线程更好地
 * 利用多个处理器机器和允许阻止一个执行线程,
 * 同时允许其他运行。
 * @short Thread support class
 * @short 线程支持类
 */
class YATE_API Thread : public Runnable
{
    friend class ThreadPrivate;
    friend class MutexPrivate;
    friend class SemaphorePrivate;
    YNOCOPY(Thread); // no automatic copies please
public:
    /**
     * Running priorities, their mapping is operating system dependent
     * 优先级运行,其映射是依赖于操作系统的
     */
    enum Priority {
	Lowest,
	Low,
	Normal,
	High,
	Highest
    };

    /**
     * This method is called when the current thread terminates.
     * 当前线程终止时调用此方法。
     */
    virtual void cleanup();

    /**
     * Actually starts running the new thread which lingers after creation
     * 实际上开始运行后残留的新线程创建
     * @return False if an error occured, true if started ok
     * @返回false,如果运行错误,true,如果启动成功
     */
    bool startup();

    /**
     * Check if the thread creation failed
     * 检查线程是否创建失败
     * @return True if an error occured, false if created ok
     * @返回false,如果运行错误,true,如果启动成功
     */
    bool error() const;

    /**
     * Check if the thread is running or not
     * 检查线程是否正在运行
     * @return True if running, false if it has terminated or no startup called
     * @返回true,正在运行,false,线程结束或者还没有开始运行
     */
    bool running() const;

    /**
     * Count how many Yate mutexes are kept locked by this thread
     * 统计当期线程互斥锁的个数
     * @return Number of Mutex locks held by this thread
     * @返回线程互斥锁的个数
     */
    inline int locks() const
	{ return m_locks; }

    /**
     * Check if the thread is currently helding or attempting to lock a mutex
     * 检查当前互斥锁的个数或者正要锁的
     * @return True if the current thread is in an unsafe to cancel state
     * @返回true,如果当前的线程在一个不安全的取消状态
     */
    inline bool locked() const
	{ return m_locking || m_locks; }

    /**
     * Get the name of this thread
     * 获得这个线程的名称
     * @return The pointer that was passed in the constructor
     * @返回构造函数的指针
     */
    const char* name() const;

    /**
     * Get the name of the currently running thread
     * 获得当前正在运行的线程名称
     * @return The pointer that was passed in the thread's constructor
     * @返回构造函数的指针
     */
    static const char* currentName();

    /**
     * Give up the currently running timeslice. Note that on some platforms
     *  it also sleeps for the operating system's scheduler resolution
     * 放弃当前运行的时间片。请注意,在某些平台上
     * 也睡操作系统调度器的决议
     * @param exitCheck Terminate the thread if asked so
     * @参数exitCheck,如果线程正在被请求也结束
     */
    static void yield(bool exitCheck = false);

    /**
     * Sleep for a system dependent period adequate for an idle thread.
     * 睡眠系统依赖时间足够的空闲线程。
     * On most operating systems this is a 5 msec sleep.
     * 在大多数操作系统这是一个5毫秒的睡眠。
     * @param exitCheck Terminate the thread if asked so
     * @参数exitCheck,如果线程正在被请求也结束
     */
    static void idle(bool exitCheck = false);

    /**
     * Sleep for a number of seconds
     * 睡眠指定的秒数
     * @param sec Number of seconds to sleep
     * @参数sec,要睡眠的秒数
     * @param exitCheck Terminate the thread if asked so
     * @参数exitCheck,如果线程正在被请求也结束
     */
    static void sleep(unsigned int sec, bool exitCheck = false);

    /**
     * Sleep for a number of milliseconds
     * 睡眠指定的毫秒数
     * @param msec Number of milliseconds to sleep
     * @参数msec,要睡眠的毫秒数
     * @param exitCheck Terminate the thread if asked so
     * @参数exitCheck,如果线程正在被请求也结束
     */
    static void msleep(unsigned long msec, bool exitCheck = false);

    /**
     * Sleep for a number of microseconds
     * 睡眠指定的微秒数
     * @param usec Number of microseconds to sleep, may be rounded to
     *  milliseconds on some platforms
     * @参数usec,要睡眠的微秒数,在有些平台是微秒
     * @param exitCheck Terminate the thread if asked so
     * @参数exitCheck,如果线程正在被请求也结束
     */
    static void usleep(unsigned long usec, bool exitCheck = false);

    /**
     * Get the platform dependent idle sleep interval in microseconds
     * 睡眠依赖于平台空闲间隔在微秒
     * @return Number of microseconds each call to idle() will sleep
     * @返回的微秒每个调用闲置()将睡眠
     */
    static unsigned long idleUsec();

    /**
     * Get the platform dependent idle sleep interval in milliseconds
     * 睡眠依赖于平台空闲间隔在毫秒
     * @return Number of milliseconds each call to idle() will sleep
     * @返回的毫秒每个调用闲置()将睡眠
     */
    static unsigned long idleMsec();

    /**
     * Set the idle sleep interval or reset to platform default
     * 设置空闲睡眠间隔或重置为默认平台
     * @param msec Sleep interval in milliseconds, platform default if zero
     * @参数msec,睡眠时间间隔以毫秒为单位,平台默认如果零
     */
    static void idleMsec(unsigned long msec);

    /**
     * Get a pointer to the currently running thread
     * 获得当前运行的线程的指针
     * @return A pointer to the current thread or NULL for the main thread
     *  or threads created by other libraries
     * @返回当前线程或NULL指针主线程或其他库创建的线程
     */
    static Thread* current();

    /**
     * Get the number of Yate created threads
     * 获得线程创建的个数
     * @return Count of current Thread objects
     * @返回当先线程对象的计数
     */
    static int count();

    /**
     * Check if the current thread was asked to terminate.
     * 核查当前的线程是否被要求结束
     * @param exitNow If thread is marked as cancelled then terminate immediately
     * @参数exitNow,如果线程标记为取消,立即终止
     * @return False if thread should continue running, true if it should stop
     */
    static bool check(bool exitNow = true);

    /**
     * Terminates the current thread.
     * 结束当前的线程
     */
    static void exit();

    /**
     * Terminates the specified thread.
     * 结束指定的线程
     * @param hard Kill the thread the hard way rather than just setting an exit check marker
     * @参数hard,采用强硬的方式杀死线程而不仅仅是设置退出检查标记
     */
    void cancel(bool hard = false);

    /**
     * Check if this thread is the currently running thread
     * 见这个线程是不是正在运行的线程
     * @return True if this is the current thread
     * @放回为true,是当前的线程
     */
    inline bool isCurrent() const
	{ return current() == this; }

    /**
     * Get the object counter of this thread
     * 获得线程的对象计数
     * @return Pointer to thread's counter for new objects
     * @返回指向线程的计数器为新对象
     */
    NamedCounter* getObjCounter() const;

    /**
     * Set the object counter of this thread
     * 设置线程的对象计数
     * @param counter New counter object or NULL
     * @参数counter,新的对象或者NULL
     * @return Pointer to old counter object
     * @返回旧对象的计数
     */
    NamedCounter* setObjCounter(NamedCounter* counter);

    /**
     * Get the object counter of the current thread
     * 获得当前线程的对象计数
     * @param always Return the object even if counting is disabled
     * @参数always,返回的对象即使计数是禁用的
     * @return Pointer to current counter for new objects
     * @返回指针为新对象当前的计数器
     */
    static NamedCounter* getCurrentObjCounter(bool always = false);

    /**
     * Set the object counter of the current thread
     * 设置当前线程的对象计数
     * @param counter New counter object or NULL
     * @参数counter,新的对象或者NULL
     * @return Pointer to old counter object
     * @返回旧对象的计数
     */
    static NamedCounter* setCurrentObjCounter(NamedCounter* counter);

    /**
     * Convert a priority name to a thread priority level
     * 转成线程的优先级的名称为级别
     * @param name Name of the requested level
     * @参数name,别请求的级别名称
     * @param defvalue Priority to return in case of an invalid name
     * @参数defvalue,如果是一个无效的名称返回默认值
     * @return A thread priority level
     * @返回线程的优先级别
     */
    static Priority priority(const char* name, Priority defvalue = Normal);

    /**
     * Convert a priority level to a textual name
     * 转成线程的优先级的级别为文本名称
     * @param prio Priority level to convert
     * @参数prio,要转换的优先级的级别
     * @return Name of the level or NULL if an invalid argument was provided
     * @返回级别的名称或NULL如果一个无效的参数
     */
    static const char* priority(Priority prio);

    /**
     * Kills all other running threads. Ouch!
     * 杀死所有其他线程运行
     * Must be called from the main thread or it does nothing.
     * 必须从主线程调用或什么也不执行。
     */
    static void killall();

    /**
     * On some platforms this method kills all other running threads.
     * Must be called after fork() but before any exec*() call.
     */
    static void preExec();

    /**
     * Get the last thread error
     * @return The value returned by GetLastError() (on Windows) or
     *  the value of C library 'errno' variable otherwise
     */
    static int lastError();

    /**
     * Get the last thread error's string from system.
     * 从系统获取最后的线程错误的信息
     * @param buffer The destination string
     * @参数buffer,用于存放错误信息
     * @return True if an error string was retrieved. If false is returned, the buffer
     *  is filled with a generic string indicating an unknown error and its code
     * @返回true,错误信息被找到,false,未知的错误和错误码
     * @缓冲区被通用的字符填满
     */
    static inline bool errorString(String& buffer)
	{ return errorString(buffer,lastError()); }

    /**
     * Get an error string from system.
     * 从系统获取错误的信息
     * On Windows the code parameter must be a code returned by GetLastError().
     * 在windows系统时通过GetLastError()来获取的错误码
     * Otherwise, the error code should be a valid value for the C library 'errno'
     * 此外,都是通过c库的errno错误码
     *  variable
     * @param buffer The destination string
     * @参数buffer,用于存放错误信息
     * @param code The error code
     * @参数code,用于存放错误码
     * @return True if an error string was retrieved. If false is returned, the buffer
     *  is filled with a generic string indicating an unknown error and its code
     * @返回true,错误信息被找到,false,未知的错误和错误码
     * @缓冲区被通用的字符填满
     */
    static bool errorString(String& buffer, int code);

protected:
    /**
     * Creates and starts a new thread
      创建和启动一个新的线程
     * @param name Static name of the thread (for debugging purpose only)
     * @参数name,静态的线程名(仅用于调试)
     * @param prio Thread priority
     * @参数prio,线程运行的优先级
     */
    Thread(const char *name = 0, Priority prio = Normal);

    /**
     * Creates and starts a new thread
     * 创建和启动一个新的线程
     * @param name Static name of the thread (for debugging purpose only)
     * @参数name,静态的线程名(仅用于调试)
     * @param prio Thread priority level name
     * @参数prio,线程运行的优先级
     */
    Thread(const char *name, const char* prio);

    /**
     * The destructor is called when the thread terminates
     * 当这个线程结束的时候调用这个析构函数
     */
    virtual ~Thread();

private:
    ThreadPrivate* m_private;
    int m_locks;
    bool m_locking;
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值