google base库之simplethread

// This is the base SimpleThread.  You can derive from it and implement the
// virtual Run method, or you can use the DelegateSimpleThread interface.
class BASE_EXPORT SimpleThread : public PlatformThread::Delegate {
 public:
  class BASE_EXPORT Options {
   public:
    Options() : stack_size_(0) { }
    ~Options() { }

    // We use the standard compiler-supplied copy constructor.

    // A custom stack size, or 0 for the system default.
    void set_stack_size(size_t size) { stack_size_ = size; }
    size_t stack_size() const { return stack_size_; }
   private:
    size_t stack_size_;
  };

  // Create a SimpleThread.  |options| should be used to manage any specific
  // configuration involving the thread creation and management.
  // Every thread has a name, in the form of |name_prefix|/TID, for example
  // "my_thread/321".  The thread will not be created until Start() is called.
  explicit SimpleThread(const std::string& name_prefix);
  SimpleThread(const std::string& name_prefix, const Options& options);

  virtual ~SimpleThread();

  virtual void Start();
  virtual void Join();

  // Subclasses should override the Run method.
  virtual void Run() = 0;

  // Return the thread name prefix, or "unnamed" if none was supplied.
  std::string name_prefix() { return name_prefix_; }

  // Return the completed name including TID, only valid after Start().
  std::string name() { return name_; }

  // Return the thread id, only valid after Start().
  PlatformThreadId tid() { return tid_; }

  // Return True if Start() has ever been called.
  bool HasBeenStarted();

  // Return True if Join() has evern been called.
  bool HasBeenJoined() { return joined_; }

  // Overridden from PlatformThread::Delegate:
  virtual void ThreadMain() OVERRIDE;

  // Only set priorities with a careful understanding of the consequences.
  // This is meant for very limited use cases.
  void SetThreadPriority(ThreadPriority priority) {
    PlatformThread::SetThreadPriority(thread_, priority);
  }

 private:
  const std::string name_prefix_;
  std::string name_;
  const Options options_;
  PlatformThreadHandle thread_;  // PlatformThread handle, invalid after Join!
  WaitableEvent event_;          // Signaled if Start() was ever called.
  PlatformThreadId tid_;         // The backing thread‘s id.
  bool joined_;                  // True if Join has been called.
};

  特别说明,由于需要快速学习,所以,我的文章中有些关于记忆的东西。

  这个类相对还是比较简单,就是对线程的简单封装,相比boost的thread简直不知简单到哪里去,不过无所谓,简单能办事就可以。

  重写run函数实现自己的逻辑

  // Subclasses should override the Run method.
  virtual void Run() = 0;

  值得注意的就是下面这个数据成员

  WaitableEvent event_;          // Signaled if Start() was ever called.

  到是注解中就说得很明白了,必需要start调用了这个event才置信。这个类先不管它吧,后面还在好戏,google的这个框架应当还是挺好用的,必竟是将thread的windows的消息循环接合得比较紧密,后续文章再一一解开。

时间: 2024-08-09 08:47:46

google base库之simplethread的相关文章

google base库中的WaitableEvent

这个类说白了就是对windows event的封装,没有什么特别的,常规做法,等侍另一线程无非就是等侍事件置信waitsingleobject,通知事件无非就是setevent,一看就明白,不就详解,写在这里只是为了这个系更的完整性 下边的示例代码注意下: // WaitableEvent *e = new WaitableEvent; // SendToOtherThread(e); // e->Wait(); // delete e;  SendToOtherThread(e); 这个应当就

解决jquery库和base库的冲突

jquery库引用在base库之前,$的所有权就是base库的:而jquery库引用在base库之前后的话,$的所有权就是jquery库的.解决这种库之间的冲突可用以下方法解决: 情况一,jquery库引用在base库之前 比如: //var $$ = jQuery; $(funtion(){       //jquery库引用在base库之前,$的所有权就是base库的 alert($("#box").ge(0)); //ge()方法属于base而不属于jquery alert(jQ

base库插件---拖动

1 /** 2 * Created by Administrator on 2014/6/5 0005. Base-drag 基于Base库的拖拽插件 tags为你要拖拽的元素参数, 数组形式传入 3 */ 4 5 $().extend('drag', function () { 6 var tags = arguments; 7 for (var i = 0; i < this.elements.length; i ++) { 8 addEvent(this.elements[i], 'mou

google base 之MessagePumpForUI

base库中比较有意思就是这个类了,如同很多界面库一样,创建了一个隐藏窗口来处理需要在界面线程处理的消息,大体原理也就是需要执行task的时候发送一个自定义的消息,当窗口接收到task的时候调用保存起来的回调函数,还有的是通过把回调放在消息结构体里面 自下义的消息 // Message sent to get an additional time slice for pumping (processing) another // task (a series of such messages c

Google Guava 库用法整理&lt;转&gt;

参考: http://codemunchies.com/2009/10/beautiful-code-with-google-collections-guava-and-static-imports-part-1/(2,3,4) http://blog.publicobject.com 更多用法参考http://ajoo.iteye.com/category/119082 附 guava中文api地址http://ifeve.com/google-guava/ 以前这么用: Java代码   M

base库

1 /* 2 * 跨浏览器基础库=============================================== 3 * */ 4 5 //浏览器检测 6 (function () { 7 window.sys = {}; 8 var ua = navigator.userAgent.toLowerCase(); 9 var s; 10 (s = ua.match(/msie ([\d.]+)/)) ? sys.ie = s[1] : 11 (s = ua.match(/firef

Google序列化库FlatBuffers 1.1发布,及与protobuf的比较

个人总结: FlatBuffer相对于Protobuffer来讲,优势如下: 1. 由于省去了编解码的过程,所以从速度上快于Protobuffer,个人测试结果100w次编解码,编码上FlatBuffer 优势不明显,解码上优势明显 2. FlatBuffer的格式文件定义上比Protobuffer格式更丰富 3. 使用方便,直接一个头文件就能搞定,这点很赞 劣势: 1. FlatBuffer的使用上不如Protobuffer方便,创建类型多了一次转换,这和FlatBuffer提升性能有关 2.

google base之LockImpl

为了兼容不同的平台,这个类采用了impl模式,win平台通过CRITICAL_SECTION, 这样的话还是相对比较简单,具体就不详解了,不过不得不说boost的实现方式就要复杂到哪里去了,当然,好处就是功能强大,坏处就是学习成本高,会用的人少. #if defined(OS_WIN) #include <windows.h> #elif defined(OS_POSIX) #include <pthread.h> #endif #include "base/base_e

google base之IncomingTaskQueue

如同名称描述的那样,这个类就是个taskqueue,也就是任务队列,添加任务到队列,然后由MessageLoop去执行task,比较关心的函数如下: bool IncomingTaskQueue::AddToIncomingQueue( const tracked_objects::Location& from_here, const Closure& task, TimeDelta delay, bool nestable) { AutoLock locked(incoming_queu