⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.h

📁 遗传算法的一个库
💻 H
字号:
#ifndef __GradSoft_Thread_h#define __GradSoft_Thread_h/* * part of GradSoft C++ ToolBox * (C) GradSoft 2000, 2001 * http://www.gradsoft.com.ua * $Id: Thread.h,v 1.29 2001/10/31 03:13:47 rssh Exp $ */#ifndef __GradSoft_RWLock_h#include <GradSoft/RWLock.h>#endif#ifdef WIN32#include <windows.h>#endif/** * **/namespace GradSoft{/** * Base class for thread. * Library users create own threads by inherienting from this class * and overriding run() method.  **/class Thread{public:#ifdef HAVE_PTHREAD_H  typedef pthread_t id_type;#endif#ifdef WIN32  typedef HANDLE id_type;#endifprivate:  id_type id_;  int error_code_;    volatile bool running_;  RWLock runningRWLock_;#ifdef HAVE_PTHREAD_H  pthread_attr_t attr_;  bool in_cleanup_;  pthread_mutex_t idMutex_;#endif#ifdef WIN32  DWORD threadID_;  bool do_break_;  Mutex idMutex_;#endif  /**   * join current thread with argument.   * note, that all thread must be joined   * with main thread after completition.   * By default this is done in thread   * destructor   **/  static void join(const id_type& id) throw();public:  ///  Thread() throw(ThreadingExceptions::NoMemory);  ///  virtual ~Thread() ;  /**   * return system-depended id of started thread   **/  id_type id() const  throw()         {#ifdef WIN32    return (HANDLE)threadID_;#else    return id_; #endif  }  /**   * return id of current thread   **/  static id_type current_id()  throw();  /**   * return error code of last thread operation;   * 0 if all is good, otherwise - system depended    * nonzero value   **/  int error_code() const throw () { return error_code_; }  /**   * library user put must put all work of thread here.   **/  virtual void run()=0;  /**   * Is thread running ?   *@return true, if thread is running; false otherwise.   **/  bool  is_running() const throw (ThreadingExceptions::NoResources,                                  ThreadingExceptions::InternalError);  /**   * start thread.   **/  void start()             throw (ThreadingExceptions::NoResources,                   ThreadingExceptions::InternalError);  /**   * cancel thread   **/  void cancel() throw();  /**   * join current thread with thread <code> th </code>   **/  static void join(const Thread& th) throw()    { join(th.id_); }  /**   * sleep, yield and test cancelation points   **/  static void sleepCurrent(unsigned int sec) throw();  /**   * sleep, yield and test cancelation points   **/  static void nanosleepCurrent(unsigned int sec, long nsec) throw();  /**   * yield   **/  static void yield();protected:  /**   * exit from thread   **/  void exit(void) throw();  /**   * test for cancelation point   **/  void testcancel(void) throw();  /**   * sleep, yield and test for cancelation point   **/  void sleep(unsigned int sec) throw();  /**   * sleep, yield and test for cancelation point   **/  void nanosleep(unsigned int sec, long nsec) throw();private:  static void* thread_process(void* v) throw();  static void  thread_cleanup_process(void* v) ;  // block copying  Thread(const Thread& x);  Thread& operator=(const Thread& x);};}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -