1// excerpts from http://code.google.com/p/muduo/
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the License file.
5//
6// Author: Shuo Chen (giantchen at gmail dot com)
7
8#ifndef MUDUO_BASE_THREAD_H
9#define MUDUO_BASE_THREAD_H
10
11#include "Atomic.h"
12
13#include <boost/function.hpp>
14#include <boost/noncopyable.hpp>
15#include <boost/shared_ptr.hpp>
16#include <pthread.h>
17
18namespace muduo
19{
20
21class Thread : boost::noncopyable
22{
23 public:
24  typedef boost::function<void ()> ThreadFunc;
25
26  explicit Thread(const ThreadFunc&, const std::string& name = std::string());
27  ~Thread();
28
29  void start();
30  void join();
31
32  bool started() const { return started_; }
33  // pthread_t pthreadId() const { return pthreadId_; }
34  pid_t tid() const { return *tid_; }
35  const std::string& name() const { return name_; }
36
37  static int numCreated() { return numCreated_.get(); }
38
39 private:
40  bool        started_;
41  bool        joined_;
42  pthread_t   pthreadId_;
43  boost::shared_ptr<pid_t> tid_;
44  ThreadFunc  func_;
45  std::string name_;
46
47  static AtomicInt32 numCreated_;
48};
49
50namespace CurrentThread
51{
52  pid_t tid();
53  const char* name();
54  bool isMainThread();
55}
56
57}
58
59#endif
60